Python Lab 5: if statements with else and elif 1. Write an if statement to determine if a number is divisible by 5. Ask the user to enter a number. Determine if the number is divisible by 5. if (number % 5 == 0): print (str(number) + ' is divisible by 5') 2. Write an if/else statement. An else statement can follow an if statement. The code in an else statement is executed if the expression in the if statement is false. if (number % 5 == 0): print (str(number) + ' is divisible by 5') else: print (str(number) +' is divisible by 5') Notice that an else statement does not have a Boolean expression. Write an additional if/else statement to see if the number is divisible by 2. Test your code with many different inputs. 3. Write an iflelif/else The command elif can be used to make a series of if statements more efficient. As soon as one of the expressions is true, the other ones are skipped. elif is short for 'else if. sequence. Ask the user to enter a State/Province name, and print out the capital of that state. Your code should handle at least 6 different inputs. Use an if/elif/else structure if (state == 'Wisconsin'): print ('Madison') elif (state == 'Colorado)': print('Denver') else: print ('I do not know that one') Test your code with many different input values. 4. Use elif in a function definition. At a certain public pool, entrance prices are as follows: Under 2 years: free, Age 2-11: $3, Age 11-60: $6, Over 60: $4 Complete a function that takes the age in years as input and returns the price in dollars (without the dollar sign). def pool_admission(age): if (age < 2): return e elif (age < 12): return 3 Notice that elif reduces possibilities and makes the code easier to read.
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 15 images