If Statements¶
if statements have this general form:
if conditional :
body
and
if-else statements have this general form:
if conditional :
if-body
else:
else-body
and
if-elif-else statements have this general form:
if conditional :
if-body
elif conditional :
elif-body
else:
else-body
-
Write an if statement that executes the body only if
v1is less thanv2. The body should simply containprint('yes').Look at the top of this file for a reminder about the formatting of an if statement.
You do not need an else part.
-
Write an if statement that prints
yesifv1is greater thanv2and printsnootherwise.Look at the top of this file for a reminder about the formatting of an if-else statement.
You need an else part.
-
Write an if statement that prints
yesifv1is less thanv2andv3is equal tov4. Nothing is printed otherwise.Use
andto join the two boolean expressions.You do not need an else part.
Don’t forget to use
==, not=(which is assignment).
-
If
v1= 1, andv2= 2, create string variableresStrwhich refers tovariable 1 is 1 (1.0) and variable 2 is 2 (2.0).To make sure you have only 1 digit after the decimal, use this format specifier:
%.1fYou can use a variable multiple times in the same tuple.
-
Write an if statement that prints
is not 3 timesifv1is not equal to 3 times the value ofv4.To make sure you have only 1 digit after the decimal, use this format specifier:
%.1fNot equal to is
!=.
-
Write an if statement that assigns
resto ‘even number’ if variableidxis an even number andresto ‘odd number’ otherwise.You can tell if a value is even if when you divide it by 2, the remainder is 0.
-
Write an if statement that assigns
resto 1 if variablev1is strictly between 100 and 200.Use
<and>andand.Remember that you have to have a value, variable, or expression on each side of a
<or>. You cannot writev1 > 100 and < 200because there is no expression on the left side of the<.
-
Write an if statement that assigns
leaptoTrueif variableyearis a multiple of 4 and is not a multiple of 100, and toFalseotherwise.A numbernis a multiple of another numberd, if when you dividenbyd, the remainder is 0.
-
Write an if statement that assigns 1 to
answerif variableresulthas the value100, and setsanswerto 2 otherwise.Use an if-else statement.
-
Write an ifelse statement that sets the value of a variable
gradepointto 4 if a variablescoreis greater than 90, 3 ifscoreis between 80 and 89, 2 ifscoreis between 70 and 79, 1 ifscoreis between 60 and 69, and 0 otherwise.Use an if-elif-else statement.
-
Write a short program that first asks the user to input 3 numbers. (Do this with 3 individual
input()calls, storing the results in 3 variables.) Then, the program prints out the largest of the 3 numbers. You may assume the user enters three distinct values.Use multiple if/elif statements, each with multiple boolean operators connected together withands.
-
Write a short program that asks the user for a payrate and a number of hours, and then prints out the total pay. Any hours over 40 are paid with time and a half.Use an if-else statement.
-
On a roulette wheel, the pockets are numbered from 0 to 36. The colors of the pockets are as follows:
- Pocket 0 is green.
- For pockets 1 through 10, the odd-numbered pockets are red and the even-numbered pockets are black.
- For pockets 11 through 18, the odd-numbered pockets are black and the even-numbered pockets are red.
- For pockets 19 through 28, the odd-numbered pockets are red and the even-numbered pockets are black.
- For pockets 29 through 36, the odd-numbered pockets are black and the even-numbered pockets are red.
Write a program that asks the user to enter a pocket number and displays whether the pocket is green, red, or black. The program should display an error message if the user enters a number that is outside the range of 0 through 36.
Use an if-elif-else statement with nested if statements in each part. An odd number is one where when you divide by 2, the remainder is 1.
-
Write a program that asks the user to enter two numbers and an operator to apply to them. The operator must be one of
+,-,*,/,//, or%. The program applies the operator to the numbers and prints the result.Here is an example run:
Enter first number:
46Enter second number:
13.5Enter an operator (one of +, -, *, /, //, %):
-46.0000000 - 13.5000000 = 32.500000If the user enters an illegal operator, the program prints an error message.
Use an if-elif-else statement. The print statement for the + operator looks like this:
print('%f + %f = %f' % (num1, num2, num1 + num2))