Computer Programming Exercise 4: What is the purpose of the “def” keyword in Pyt
ID: 3873271 • Letter: C
Question
Computer Programming
Exercise 4: What is the purpose of the “def” keyword in Python?
a) It is slang that means “the following code is really cool”
b) It indicates the start of a function
c) It indicates that the following indented section of code is to be stored for later
d) b and c are both true
e) None of the above
Exercise 5: What will the following Python program print out?
def fred():
print("Zap")
def jane():
print("ABC")
jane()
fred()
jane()
a) Zap ABC jane fred jane
b) Zap ABC Zap
c) ABC Zap jane
d) ABC Zap ABC
e) Zap Zap Zap
Exercise 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).
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
Exercise 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.
Score Grade
> 0.9 A
> 0.8 B
> 0.7 C
> 0.6 D
<= 0.6 F
Program Execution:
Enter score: 0.95
A
Enter score: perfect
Bad score
Enter score: 10.0
Bad score
Enter score: 0.75
C
Enter score: 0.5
F
Run the program repeatedly to test the various different values for input.
Explanation / Answer
1)Answer: b)It indicates the start of a function
Explanation:
def keyword indicates the start of a function.
5)Answer: d) ABC Zap ABC
Explanation:
Here
jane(): = print("ABC") and
fred(): = print("Zap")
we want to print
jane()
fred()
jane()
so it will print d) ABC Zap ABC.
6)Answer:
print "Pay: %.2f" % computepay(hours, rate)
7)Answer:
import sys hours = raw_input("Enter Hours: ") rate = raw_input("Enter Rate: ") OVERTIME_RATE = 1.5 try: hours = float(hours) rate = float(rate) except: print "Please enter valid input" sys.exit(1) def computepay(hours, rate): if hours > 40: overtime_hours = hours - 40 # hours over 40 hours -= overtime_hours # regular rate hours overtime_pay = overtime_hours * rate * OVERTIME_RATE return (hours * rate) + overtime_pay else: return (hours * rate)print "Pay: %.2f" % computepay(hours, rate)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.