#example of loop """import random and run iteration""" for i in range(10): x = random.random() print x def print_lyrics(): print "I'm a lumberjack and I'm okay." print "I sleep all night and I work all day" print_lyrics() def repeat_lyrics(): print_lyrics() print_lyrics() repeat_lyrics() def print_twice(info_in): print name print name info_in = raw_input("Please enter your name: ") print "" print_twice(info_in) #Exercise 4.6 """rewrite pay computation using function computepay(hours, rate)""" def computepay(hours, rate): f_hours = float(hours) f_rate = float(rate) if hours > 40.0: ot_pay = (f_hours - 40.0) * (f_rate * 1.5) reg_pay = f_hours * f_rate gross_pay = ot_pay + reg_pay print "Your gross pay is ", gross_pay else: gross_pay = f_hours * f_rate print "Your gross pay is", gross_pay hours = raw_input("Please enter your hours: \n") print hours rate = raw_input("Please enter your payrate: \n") print rate computepay(hours, rate) #exercise 4.7 """rewrite computegrade using function""" def computegrade(num_grade): f_num_grade = float(num_grade) if f_num_grade >= 0.9 and f_num_grade <= 1.0: print "The grade is an A" elif f_num_grade >= 0.8 and f_num_grade < 0.9: print "The grade is a B" elif f_num_grade >= 0.7 and f_num_grade < 0.8: print "The grade is a C" elif f_num_grade >= 0.6 and f_num_grade < 0.7: print "The grade is a D" elif f_num_grade < 0.6: print "The grade is a F" else: print "The number is out of range" num_grade = raw_input("Please enter the numeric grade: ") print num_grade computegrade(num_grade)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question