Unit 4 Flashcards
What does this program print?
favorite_color = “blue”
if favorite_color != “blue”:
if favorite_color != “red”:
print(“Must be yellow”)
elif favorite_color == “blue”:
if favorite_color != “yellow”:
print(“Must be blue”)
elif favorite_color == “blue”:
if favorite_color != “yellow”:
print(“I like purple”)
else:
if favorite_color == “blue”:
print(“Blue is the best”)
Must be blue
How would you round the number held in the variable pi to 3.14?
pi = 3.14159265
round(pi, 2)
Why should you use round when you compare two numbers that have type float?
Sometimes numbers can be rounded in unexpected ways based on how Python computes them. Therefore, it is best to use round in order to make sure both numbers are rounded the same way.
What will print to the screen when the following program is run?
number = 5
greater_than_zero = number > 0
print(type(number))
<type ‘int’>
What will print to the screen when the following program is run?
number = 5
less_than_zero = number < 0
print(type(less_than_zero)
<type ‘bool’>
What will be the output of this program?
number = 5
greater_than_zero = number > 0
if greater_than_zero:
print(number)
5
number = 5
greater_than_zero = number > 0
if greater_than_zero:
if number > 5:
print(number)
Nothing will print
number = 5
less_than_zero = number < 0
if less_than_zero:
print(number)
number = number-10
less_than_zero = number < 0
if less_than_zero:
print(number)
-5
number = 5
greater_than_zero = number > 0
less_than_zero = number < 0
if greater_than_zero:
print(number)
if less_than_zero:
print(number + 1)
if greater_than_zero and less_than_zero:
print(number + 2)
if greater_than_zero or less_than_zero:
print(number + 3)
5
8
above_16 = True
has_permit = True
passed_test = False
if above_16 and has_permit and passed_test:
print(“Issue Driver’s License”)
elif above_16 or has_permit or passed_test:
print(“Almost eligible for Driver’s License”)
else:
print(“No requirements met.”)
Almost eligible for Driver’s License
number_one = 5
number_two = 10
if number_one == 5:
print(1)
if number_one > 5:
print(2)
if number_two < 5:
print(3)
if number_one < number_two:
print(4)
if number_one != number_two:
print(5)
1
4
5
number_one = 5
number_two = 10
if number_one == 5:
print(1)
elif number_one > 5:
print(2)
elif number_two < 5:
print(3)
elif number_one < number_two:
print(4)
elif number_one != number_two:
print(5)
else:
print(6)
1
height = 65
gender = “female”
if height < 62:
print(“You are below average height”)
elif height > 69:
print(“You are above average height”)
else:
print(“You are average height”)
You are average height
height = 65
gender = “female”
if height < 62 or (height < 69 and gender == “male”):
print(“You are below average height”)
elif height > 69 or (height > 62 and gender == “female”):
print(“You are above average height”)
else:
print(“You are average height”)
You are above average height