-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathexercise3_2.py
executable file
·41 lines (34 loc) · 1.13 KB
/
exercise3_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
"""
Exercise 3.2: Rewrite your pay program using try and except so that your
program handles non-numerical 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
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
"""
hours = 0.0 # Initialize variables
rate = 0.0
pay = 0.0
input_hours = input('Enter Hours: ')
try:
hours = float(input_hours) # Only allows input floats
except ValueError:
print('Error, please enter numeric input')
quit()
input_rate = input('Enter Rate: ')
try:
rate = float(input_rate) # Only allows input floats
except ValueError:
print('Error, please enter numeric input')
quit()
if hours < 40:
pay = rate * hours # No overtime calculation
else:
overtime = hours - 40 # Calculate amount of overtime
pay = (rate * 40.0) + (1.5 * rate * overtime)
print('Pay: ', pay)