Advertisement
oldmagi

Chapter 3

Sep 7th, 2012
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. #Exercise 3.1 Rewrite your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours.
  2. h=input('Enter Hours:')
  3. r=input('Enter rate:')
  4. if int(h)>40:
  5.     extra=float(r)*float((int(h)-40)*1.5)
  6.     pay=(float(r)*float(40))+extra
  7. else:
  8.     pay=float(h)*float(r)
  9.  
  10. print(pay)
  11.  
  12. ------------------------------------------------------------------------------------------------
  13. #Exercise 3.2 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:
  14. h=input('Enter Hours:')
  15. r=input('Enter rate:')
  16. try:
  17.     if int(h)!=str() or int(r)!=str():
  18.         if int(h)>40:
  19.           extra=float(r)*float((int(h)-40)*1.5)
  20.           pay=(float(r)*float(40))+extra
  21.           print(pay)
  22.         else:
  23.           pay=float(h)*float(r)
  24.           print(pay)    
  25. except:
  26.     print('Error, please enter numeric input')
  27.  
  28. ------------------------------------------------------------------------------------------------
  29. #Exercise 3.3 Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range print an error. If the score is between 0.0 and 1.0, print a grade using the following table:
  30. i=input('Enter Score:')
  31. try:
  32.     h=float(i)
  33.     if h!=str():
  34.         if h>=0.0 and h<=1.0:
  35.             if h>=0.6 and h<0.7:
  36.                 print('D')
  37.             elif h>=0.7 and h<0.8:
  38.                 print('C')
  39.             elif h>=0.8 and h<0.9:
  40.                 print('B')
  41.             elif h>=0.9:
  42.                 print('A')
  43.             else:
  44.                 print('F')
  45.         else:
  46.             print('Bad score')
  47. except:
  48.     print('Bad score')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement