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
v1
is 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
yes
ifv1
is greater thanv2
and printsno
otherwise.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
yes
ifv1
is less thanv2
andv3
is equal tov4
. Nothing is printed otherwise.Use
and
to 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 variableresStr
which 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:
%.1f
You can use a variable multiple times in the same tuple.
-
Write an if statement that prints
is not 3 times
ifv1
is not equal to 3 times the value ofv4
.To make sure you have only 1 digit after the decimal, use this format specifier:
%.1f
Not equal to is
!=
.
-
Write an if statement that assigns
res
to ‘even number’ if variableidx
is an even number andres
to ‘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
res
to 1 if variablev1
is 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 < 200
because there is no expression on the left side of the<
.
-
Write an if statement that assigns
leap
toTrue
if variableyear
is a multiple of 4 and is not a multiple of 100, and toFalse
otherwise.A numbern
is a multiple of another numberd
, if when you dividen
byd
, the remainder is 0.
-
Write an if statement that assigns 1 to
answer
if variableresult
has the value100
, and setsanswer
to 2 otherwise.Use an if-else statement.
-
Write an ifelse statement that sets the value of a variable
gradepoint
to 4 if a variablescore
is greater than 90, 3 ifscore
is between 80 and 89, 2 ifscore
is between 70 and 79, 1 ifscore
is 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 withand
s.
-
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:
46
Enter second number:
13.5
Enter an operator (one of +, -, *, /, //, %):
-
46.0000000 - 13.5000000 = 32.500000
If 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))