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
  1. Write an if statement that executes the body only if v1 is less than v2. The body should simply contain print('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.

  1. Write an if statement that prints yes if v1 is greater than v2 and prints no otherwise.

    Look at the top of this file for a reminder about the formatting of an if-else statement.

    You need an else part.

  1. Write an if statement that prints yes if v1 is less than v2 and v3 is equal to v4. 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).

  1. If v1 = 1, and v2 = 2, create string variable resStr which refers to variable 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.

  1. Write an if statement that prints is not 3 times if v1 is not equal to 3 times the value of v4.

    To make sure you have only 1 digit after the decimal, use this format specifier: %.1f

    Not equal to is !=.

  1. Write an if statement that assigns res to ‘even number’ if variable idx is an even number and res to ‘odd number’ otherwise.
    You can tell if a value is even if when you divide it by 2, the remainder is 0.
  1. Write an if statement that assigns res to 1 if variable v1 is strictly between 100 and 200.

    Use < and > and and.

    Remember that you have to have a value, variable, or expression on each side of a < or >. You cannot write v1 > 100 and < 200 because there is no expression on the left side of the <.

  1. Write an if statement that assigns leap to True if variable year is a multiple of 4 and is not a multiple of 100, and to False otherwise.
    A number n is a multiple of another number d, if when you divide n by d, the remainder is 0.
  1. Write an if statement that assigns 1 to answer if variable result has the value 100, and sets answer to 2 otherwise.
    Use an if-else statement.
  1. Write an ifelse statement that sets the value of a variable gradepoint to 4 if a variable score is greater than 90, 3 if score is between 80 and 89, 2 if score is between 70 and 79, 1 if score is between 60 and 69, and 0 otherwise.
    Use an if-elif-else statement.
  1. 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 with and s.
  1. 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.
  1. 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.
  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))