if / if else Problems

Use if for Problems 1 - 10

Problem 1

print(“Question 1”)

Ask the user for a number.

Write if statements to determine:

  • Is the number 0

    • If true, print the number is 0

  • Is the number 13

    • If true, print the number is 13

  • Is the number -13

    • If true, print the number is -13

Problem 2

Write Python code that asks a user for what town Kean University is located in. If the user enters “Union”, print correct.

What happens if you type “union” instead? (This is for your own knowledge)

Problem 3

Write Python code that asks a user for the amount (price) of a pack of gum. What data type is the price?

If the amount is less than 1, print "I think this is a good price". If the amount is greater than or equal to 1, print "I think this is expensive".

Problem 4

Create a variable named bills and ask the question, “What president is on the banknote?”

Print the name of the President according to the chart:

Amount

1

George Washington

2

Thomas Jefferson

5

Abraham Lincoln

10

Alexander Hamilton

20

Andrew Jackson

50

Ulysses S. Grant

100

Benjamin Franklin

Problem 5

Many people say that one human year is equivalent to seven dog years. Ask the user how old is the dog.

If the user enters less than 2, print the dog is a baby.

If the user enters 2 or more, print the dog's age in human years.

Problem 6

Write Python code that asks:

  • x1

  • x2

  • y1

  • y2

Calculate the slope of the line.

slope=y2y1x2x1slope = \frac{y2 - y1}{x2 - x1}
  • If the slope is positive, print positive slope.

  • If the slope is negative, print negative slope.

  • If the slope is zero, print horizontal line.

  • If the the denominator is 0, print vertical line.

Problem 7

Ask the user, do you hear Laurel or Yanny?

If the user enters “Laurel”, print “doesn’t it sound like Yanny?”.

If the user enters “Yanny”, print “doesn’t it sound like Laurel?”

Problem 8

Ask the user to enter an abbreviation.

  • If the user enters "lol", print "laughing out loud".

  • If the user enters "rofl", print "rolling on the floor laughing".

  • If the user enters "lmk", print "let me know".

  • If the user enters "smh", print "shaking my head".

Problem 9

Ask the user, what is the net worth of Mark Zuckerberg. (do not enter commas when entering the number)

If the user enters 54500000000, print “yes and that’s a 54.5 billion”. If the user enters less than 54500000000, print “that's not much”. If the user enters more than 54500000000, print “I'll make more when I graduate”.

Problem 10

Ask the user, who bought Minecraft for 2.5 billion dollars?

If the user enters “Microsoft”, print “correct”. If the user does not enter “Microsoft”, print “incorrect”.

Use if else for Problems 11 - 20

Problem 11

Write Python code that asks a user for two numbers. Print the number that is the greatest. Print the number that is the least. Assume the numbers are not the same.

Problem 12

Write Python code that asks a user for their password. If the user enters “1234567890”, print “that password is really easy to guess”.

Else print “good enough, I guess”.

Problem 13

Write Python code that asks a user for their temperature. If the temperature is 100.4 or more, print “you have a fever”.

Else print “normal temperature”

Problem 14

Write Python code that asks a user how many degrees is it in the Building. If the temperature is less than or equal to 70, print “feels chilly”.

Else print “comfy”.

Problem 15

The USEPA developed the Air Quality Index (AQI) to report daily air quality to the public. The AQI tells you how clean your local air is, and what associated health effects might be a concern in your area. Think of the AQI as a yardstick that runs from 0 to 300. The higher the AQI value, the greater the level of air pollution and the greater the health concern.

Write Python code that asks a user what the AQI is today. If the user enters, 50 or less, print “Air Quality is Good”. Else print, “Air Quality is Moderate or Unhealthy”.

Problem 16

Write Python code that asks a user how many pizza slices they want. The pizzeria charges $1.25 a slice if you order 10 slices or less and $1 a slice if you order more than 10 slices.

Print the total price depending on how many slices you order.

(Note: do not use the $ when assigning the price in your Python code)

Problem 17

Write Python code that asks a user how many Youtube videos they watch per day. The average video is 7 minutes. If the user enters 5 or more, print “you watch a lot of videos”.

Else print “good job!”.

Print the total number of minutes the user watches videos.

Problem 18

Write Python code that asks a user do you have a ShopRite card? Next, ask how many Apple or Pumpkin pies do they want to buy.

If the user enters “yes”, print "2.99 per pie". Else, print "4.99 per pie".

Print the total price.

Problem 19 (Optional)

Write Python code asks the user for a number. If the number is divisible by 5, print the number if it is divisible by 5. Else print the number is not divisible by 5.

The % (modulo) operator yields the remainder from the division of the first argument by the second.

Problem 20 (Optional)

Write Python code that asks a user for a number. Print if the number is even or odd.

The % (modulo) operator yields the remainder from the division of the first argument by the second.

Last updated