Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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.
- #The function random returns a random float between 0.0 and 1.0 (including 0.0 but not 1.0)
- import random
- for i in range(10):
- x=random.random()
- print(x)
- #The output of this program displays random numbers
- #0.4184357119673642
- #0.6297955102447773
- #0.6061678855186424
- #0.6153135553282525
- #0.6766973528876312
- #0.6445292394094398
- #0.8013163399117574
- #0.19139696551055974
- #0.10143315716528511
- #0.4250712145676583
- # After rerunning this program again the output was
- #0.02458150217996713
- #0.2765057775875218
- #0.21092102938726598
- #0.38367827954821265
- #0.25575302933073296
- #0.5222072687698197
- #0.6217626728991629
- #0.02943314326290125
- #0.31136277049841565
- #0.4125183265713611
- ----------------------------------------------------------------------------------------------
- #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.
- repeat_lyrics()
- def repeat_lyrics():
- print_lyrics()
- print_lyrics()
- def print_lyrics():
- print("I'm a lumberjack, and I'm okay.")
- print("I sleep all night and I work all day.")
- #output
- #Traceback (most recent call last):
- # File "/home/saravanan/Documents/python/eg4.py", line 2, in <module>
- # repeat_lyrics()
- #NameError: name 'repeat_lyrics' is not defined
- ----------------------------------------------------------------------------------------------
- #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?
- def print_lyrics():
- print("I'm a lumberjack, and I'm okay.")
- print("I sleep all night and I work all day.")
- def repeat_lyrics():
- print_lyrics()
- print_lyrics()
- repeat_lyrics()
- #No errors. The program runs successfully.
- ----------------------------------------------------------------------------------------------
- Exercise 4.4 What is the purpose of the ”def” keyword in Python?
- b) It indicates the start of a function
- ----------------------------------------------------------------------------------------------
- Exercise 4.5 What will the following Python program print out?
- d) ABC Zap ABC
- ----------------------------------------------------------------------------------------------
- #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).
- def computepay(hours,rate):
- if int(hours)>40:
- extra=float(rate)*float((int(hours)-40)*1.5)
- pay=(float(rate)*float(40))+extra
- else:
- pay=float(hours)*float(rate)
- print(pay)
- h=input('Enter Hours:')
- r=input('Enter rate:')
- computepay(h,r)
- ----------------------------------------------------------------------------------------------
- #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.
- def computegrade(i):
- try:
- h=float(i)
- if h!=str():
- if h>=0.0 and h<=1.0:
- if h>0.6 and h<=0.7:
- return 'D'
- elif h>0.7 and h<=0.8:
- return 'C'
- elif h>0.8 and h<=0.9:
- return 'B'
- elif h>0.9:
- return 'A'
- else:
- return 'F'
- else:
- return 'Bad score'
- except:
- return 'Bad score'
- s=input('Enter Score:')
- a=computegrade(s)
- print(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement