Advertisement
evita_gr

Python 101 - Lesson 4 - Chapter 4

Oct 11th, 2011
1,673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. # Exercise 4.4
  2. # What is the purpose of the ”def” keyword in Python?
  3.  
  4. # b
  5.  
  6. # Exercise 4.5
  7. # What will the following Python program print out?
  8.  
  9. # d
  10.  
  11. # Exercise 4.6
  12. # Rewrite your pay computation with
  13. # time-and-a-half for overtime and create a
  14. # function called computepay which takes two
  15. # parameters (hours and rate).
  16.  
  17. def computepay( hours, rate ):
  18.   if hours <= 40:
  19.     pay = hours * rate
  20.   else:
  21.     hours2 = hours - 40
  22.     pay = 40 * rate + 15 * hours2
  23.   print 'Pay:', pay
  24.  
  25.  
  26. hs = int(raw_input('Enter Hours:'))
  27. re = int(raw_input('Enter Rate:'))  
  28. computepay(hs, re)
  29.  
  30. # Exercise 4.7
  31. # Rewrite the grade program from the previous
  32. # chapter using a function called computegrade
  33. # that takes a score as its parameter and
  34. # returns a grade as a string.
  35.  
  36. def computegrade(score):
  37.   if score > 0.9:
  38.     return 'A'
  39.   elif score > 0.8:
  40.     return 'B'
  41.   elif score > 0.7:
  42.     return 'C'
  43.   elif score > 0.6:
  44.     return 'D'
  45.   elif score <= 0.6:
  46.     return 'F'
  47.   else:
  48.     return 'bad score'
  49.  
  50. input = raw_input('Enter score: ')
  51. try:
  52.   sc = float(input)
  53.   if sc < 10.0:
  54.     print computegrade(sc)
  55.   else:
  56.     print 'Bad score'
  57. except:
  58.   print 'Bad score'
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement