Skip to content

Review Questions

Review Questions

Q07.01 Create a program to ask a user for a temperature. If the temperature the user enters is below 50, print back the user "It is cold outside". Hint: remember to convert the user's input to a number before comparing the user's input to 50.

Q07.02 Create a program that asks the user for their temperature. If the user enters a temperature above 98.6, print back to the user "You have a fever".

Q07.03 Create a program that chooses a random number number between 1 and 5. Ask the user for a number between 1 and 5. Compare the user's number to the random number. If the user guessed the random number print "you guessed it!", if the user did not guess the random number print back to the user "try again". You can use the code below to choose a random number n between 1 and 5.

import random

n = random.randint(0,5)

Q07.04 Create a program that asks a user for two numbers, x and y. If the x is greater than y, print back to the user "x>y". If x is less than y, print back to the user "x<y". If x is equal to y, print back to the user "x=y".

Q07.05 Create a program that asks a user for one of three trig functions: sine, cosine or tangent. Calculate the sine, cosine or tangent of \pi/4 depending on the user's input and print the result of the calculation back to the user.

Q07.06 Create a program that asks the user for two numbers (use two different input lines). If the second number the user enters is zero, print back to the user "can't divide by zero", otherwise divide the user's first number by the user's second number and print the result to the user.

Q07.07 The table below shows a couple fruits and their associated color

fruit color
banana yellow
apple red
lemon yellow
lime green
orange orange

Create a program that asks a user to choose from a list of fruit. Print back to the user the color of the fruit they chose.

Q07.08 The average size of a US congressional district is about 700,000 people. Ask the user for a state population and print back to the user the number of congress members in the state. For example, a state with 1.4 million people is represented by 2 members of congress. Each state has at least one member of congress by default. If the user enters a population less than 700,000, tell the user their state only has 1 member of congress.

Q07.08 In a college engineering class, final grades are related to percentages as follows:

percentage range grade
90 - 100 A
80 - 89 B
70 - 79 C
65 - 69 D
0 - 64 F

Build a program that asks a user for a final score (in percent) and prints back to the user their letter grade.

Errors, Explanations, and Solutions

Run the following code snippets. Explain the error in your own words. Rewrite the code snippet to solve the error.

Q07.80

a = 1
if a = 0:
    print('zero')
else if a = 1:
    print('one')

Q07.81

a = 1
if a == 0:
    print('zero')

else print('one')

Q07.82

n = input('Enter a number')

if n > 0:
    print('positive')