Python 101 - Lesson 3 - Chapter 3

By: evita_gr on Oct 10th, 2011  |  syntax: Python  |  size: 1.50 KB  |  hits: 57  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. # Exercise 3.1 :
2. # Rewrite your pay computation
3. # to give the employee 1.5 times
4. # the hourly rate for hours worked
5. # above 40 hours.
6.
7. hours_1 = int(raw_input('Enter Hours:'))
8. rate_1 = int(raw_input('Enter Rate:'))
9.
10. if hours_1 <= 40:
11.   pay = hours_1 * rate_1
12. elif hours_1 > 40:
13.   hours_2 = hours_1 - 40
14.   pay = 40 * rate_1 + hours_2 * 15
15. print 'Pay:',pay
16.
17.
18. # Exercise 3.2:
19. # Rewrite your pay program using try and
20. # except so that your program handles
21. # non-numeric input gracefully by printing
22. # a message and exiting the program.
23.
24. hours_1 = raw_input('Enter Hours:')
25. try:
26.   hours = int(hours_1)
27.   rate_1 = raw_input('Enter Rate:')
28.   try:
29.     rate = int(rate_1)
30.     if hours <= 40:
31.       pay = hours * rate
32.     elif hours > 40:
33.       hours2 = hours - 40
34.       pay = 40 * rate + hours2 * 15
35.     print 'Pay:', pay
36.   except:
37.     print 'Error, please enter numeric input'
38. except:
39.   print 'Error, please enter numeric input'
40.
41.
42. # Exercise 3.3:
43. # Write a program to prompt for a score between
44. # 0.0 and 1.0. If the score is out of range print
45. # an error. If the score is between 0.0 and 1.0,
46. # print a grade using a table.
47.
48. input_b = raw_input('Enter score:')
49. try:
50.   input = float(input_b)
51.   if input >= 0.0 and input < 1.0:
52.     if input >= 0.9:
53.       print 'A'
54.     elif input >= 0.8:
55.       print 'B'
56.     elif input >= 0.7:
57.       print 'C'
58.     elif input >= 0.6:
59.       print 'D'
60.     else:
61.       print 'F'
62.   else: