Ch. 7 Flashcards
decision structures (12 cards)
a statement that controls the execution of other statements
control structure
the best structure for implementing a multi-way decision in python
if-elif-else
an expression that evaluates to either true or false
boolean
when program is being run directly (not imported), the value of __name__ is
main
the literals for type bool are
true, false
placing a decision inside of another decision
nesting
taking the square root of a negative value with math.sqrt produces a(n)
ValueError
a multiple choice decision is most likely to
multi-way decision
write a program to input number of hours worked and the hourly rate and calculate the total wages for the week
def main():
print(“Weekly pay calculator \n”)
hours = float(input(“Enter hours worked: “))
rate = float(input(“hourly wage: “))
if hours <= 40:
pay = hours * wage
else:
pay = 40 * wage + (hours-40) * 1.5 * wage
print("Your week's pay is ${0:0.2f}.".format(pay))
if __name__ == ‘__main__’:
main()
quizzes are graded on the scale 5-A, 4-B, 3-C, 2-D, 1-F, 0-F. Write a program that accepts exam score as an input and uses a decision structure to calculate the corresponding grade
def main():
print(“Quiz grade calculator \n”)
score = int(input(“Enter quiz score: “))
if score == 5:
grade = “A”
elif score == 4:
grade = “B”
elif score == 3
grade = “C”
elif score == 2
grade = “D”
else:
grade = “F”
print("The grade is ", grade".")
if __name__ == ‘__main__’:
main()
write a program that calculates a person’s BMI and prints message stating whether or not BMI is in healthy range
def main():
print(“BMI calculator \n”)
weight = float(input(“Enter weight: “))
height = float(input(“Enter height: “))
bmi = weight * 720 / height
print = (“Your BMI is “, bmi”.”)
if bmi < 19:
print(“You are below the healthy range.”)
elif bmi <= 25:
print(“You are in the healthy range.”)
else:
print(“You are above the healthy range.”)
if __name__ == ‘__main__’:
main()