Advertisement
oldmagi

Chapter 4

Sep 10th, 2012
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.74 KB | None | 0 0
  1. #Exercise 4.1 Run the program on your system and see what numbers you get. Run the program more than once and see what numbers you get.
  2.  
  3.  
  4. #The function random returns a random float between 0.0 and 1.0 (including 0.0 but not 1.0)
  5.  
  6. import random
  7.  
  8. for i in range(10):
  9.     x=random.random()
  10.     print(x)
  11.  
  12. #The output of this program displays random numbers
  13. #0.4184357119673642
  14. #0.6297955102447773
  15. #0.6061678855186424
  16. #0.6153135553282525
  17. #0.6766973528876312
  18. #0.6445292394094398
  19. #0.8013163399117574
  20. #0.19139696551055974
  21. #0.10143315716528511
  22. #0.4250712145676583
  23. # After rerunning this program again the output was
  24. #0.02458150217996713
  25. #0.2765057775875218
  26. #0.21092102938726598
  27. #0.38367827954821265
  28. #0.25575302933073296
  29. #0.5222072687698197
  30. #0.6217626728991629
  31. #0.02943314326290125
  32. #0.31136277049841565
  33. #0.4125183265713611
  34.  
  35. ----------------------------------------------------------------------------------------------
  36. #Exercise 4.2 Move the last line of this program to the top, so the function call appears beforethe definitions. Run the program and see what error message you get.
  37. repeat_lyrics()
  38.  
  39.  
  40. def repeat_lyrics():
  41.     print_lyrics()
  42.     print_lyrics()
  43.  
  44. def print_lyrics():
  45.     print("I'm a lumberjack, and I'm okay.")
  46.     print("I sleep all night and I work all day.")
  47.  
  48. #output
  49. #Traceback (most recent call last):
  50.  # File "/home/saravanan/Documents/python/eg4.py", line 2, in <module>
  51.   #  repeat_lyrics()
  52. #NameError: name 'repeat_lyrics' is not defined
  53.  
  54. ----------------------------------------------------------------------------------------------
  55. #Exercise 4.3 Move the function call back to the bottom and move the definition of print_lyrics after the definition of repeat_lyrics. What happens when you run this program?
  56.  
  57.  
  58. def print_lyrics():
  59.     print("I'm a lumberjack, and I'm okay.")
  60.     print("I sleep all night and I work all day.")
  61.  
  62. def repeat_lyrics():
  63.     print_lyrics()
  64.     print_lyrics()
  65.  
  66. repeat_lyrics()
  67.  
  68. #No errors. The program runs successfully.
  69.  
  70. ----------------------------------------------------------------------------------------------
  71. Exercise 4.4 What is the purpose of the ”defkeyword in Python?
  72. b) It indicates the start of a function
  73.  
  74. ----------------------------------------------------------------------------------------------
  75. Exercise 4.5 What will the following Python program print out?
  76. d) ABC Zap ABC
  77.  
  78. ----------------------------------------------------------------------------------------------
  79. #Exercise 4.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).
  80. def computepay(hours,rate):
  81.     if int(hours)>40:
  82.         extra=float(rate)*float((int(hours)-40)*1.5)
  83.         pay=(float(rate)*float(40))+extra
  84.     else:
  85.         pay=float(hours)*float(rate)
  86.     print(pay)
  87.  
  88.  
  89. h=input('Enter Hours:')
  90. r=input('Enter rate:')
  91. computepay(h,r)
  92.  
  93. ----------------------------------------------------------------------------------------------
  94. #Exercise 4.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.
  95.  
  96. def computegrade(i):
  97.     try:
  98.         h=float(i)
  99.         if h!=str():
  100.             if h>=0.0 and h<=1.0:
  101.                 if h>0.6 and h<=0.7:
  102.                     return 'D'
  103.                 elif h>0.7 and h<=0.8:
  104.                     return 'C'
  105.                 elif h>0.8 and h<=0.9:
  106.                     return 'B'
  107.                 elif h>0.9:
  108.                     return 'A'
  109.                 else:
  110.                     return 'F'
  111.             else:
  112.                 return 'Bad score'
  113.     except:
  114.         return 'Bad score'
  115.  
  116.  
  117. s=input('Enter Score:')
  118. a=computegrade(s)
  119. print(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement