Chapter 1-5 Exercises Flashcards
Write a program that uses input to prompt a user for their name and then welcomes them.
name = input(“Enter your name:\n”)
print(“Hello”, name)
Write a program to prompt the user for hours and rate per hour to compute gross pay.
hours = int(input(“Enter hours: “))
rate = float(input(“Enter rate: “))
pay = hours * rate
print(“Pay: “, pay)
Write a program which prompts the user for a Celsius temperature, convert the temperature to Fahrenheit, and print out the converted temperature.
cels = float(input(“Enter the temperature in Celsius: “))
fahr = cels * 1.8 + 32
print(“Temperature in Fahrenheit: “, fahr)
Rewrite your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours.
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
hours = int(input("Enter hours: ")) rate = float(input("Enter rate: ")) if hours > 40: pay = (hours - 40)*rate*1.5 + 40 * rate else: pay = hours * rate print("Pay: ", pay)
Rewrite your pay program using try and except so that your program handles non-numeric input gracefully by printing a message and exiting the program. The following shows two executions of the program:
Enter Hours: 20
Enter Rate: nine
Error, please enter numeric input
Enter Hours: forty
Error, please enter numeric input
try: hours = int(input("Enter hours: ")) rate = float(input("Enter rate: ")) except: print("Error, please enter numeric input!") quit() if hours > 40: pay = (hours - 40)*rate*1.5 + 40 * rate else: pay = hours * rate print("Pay: ", pay)
Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error message. If the score is between 0.0 and 1.0, print a grade using the following table:
Score Grade >= 0.9 A >= 0.8 B >= 0.7 C >= 0.6 D < 0.6 F
Enter score: 0.95 A Enter score: perfect Bad score Enter score: 10.0 Bad score Enter score: 0.75 C Enter score: 0.5 F
try: grade = float(input("Enter score: ")) except: print("Bad score") quit()
if grade < 0 or grade > 1.0: print("Score is out of range!") elif grade >= 0.9: print("A") elif grade >= 0.8: print("B") elif grade >= 0.7: print("C") elif grade >= 0.6: print("D") else: print("F")
Exercise 1: Write a program which repeatedly reads numbers until the user enters “done”. Once “done” is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mismistake using try and except and print an error message and skip to the next number.
Enter a number: 4 Enter a number: 5 Enter a number: bad data Invalid input Enter a number: 7 Enter a number: done 16 3 5.333333333333333
count = 0 total = 0
while True: str_val = input("Enter a number:") if str_val == 'done': break try: val = float(str_val) except: print("Invalid input") continue total = total + val count = count + 1 print(total, count, total/count)
Write another program that prompts for a list of numbers as above and at the end prints out both the maximum and minimum of the numbers instead of the average.
Enter a number: 4 Enter a number: 5 Enter a number: bad data Invalid input Enter a number: 7 Enter a number: done
count = 0
while True: str_val = input("Enter a number:") if str_val == 'done': break try: val = float(str_val) except: print("Invalid input") continue if count == 0: min_val = val max_val = val elif val < min_val: min_val = val elif val > max_val: max_val = val count = count + 1
print(min_val, max_val)
Exercise 6: Rewrite your pay computation with time-and-a-half for overtime and create a function called computepay which takes two parameters (hours and rate).
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
def computepay(hours, rate): if hours > 40: pay = (hours - 40)*rate*1.5 + 40 * rate else: pay = hours * rate return pay
hours = int(input("Enter hours: ")) rate = float(input("Enter rate: ")) pay = computepay(hours, rate)
print(“Pay: “, pay)
Exercise 7: Rewrite the grade program from the previous chapter using a function called computegrade that takes a score as its parameter and returns a grade as a string.
Score Grade >= 0.9 A >= 0.8 B >= 0.7 C >= 0.6 D < 0.6 F
Enter score: 0.95 A Enter score: perfect Bad score Enter score: 10.0 Bad score Enter score: 0.75 C Enter score: 0.5 F
Run the program repeatedly to test the various different values for input.
def computegrade(grade): if grade < 0 or grade > 1.0: return "Score is out of range!" elif grade >= 0.9: return "A" elif grade >= 0.8: return "B" elif grade >= 0.7: return "C" elif grade >= 0.6: return "D" else: return "F"
try: grade = float(input("Enter score: ")) except: print("Bad score") quit()
print(computegrade(grade))