您在這裡

Exercises

23 二月, 2015 - 14:53

Exercise 4.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 4.5 What will the following Python program print out?

def fred():    print "Zap"

def jane():    print "ABC"

jane()fred()jane()
  1. Zap ABC jane fred jane
  2. Zap ABC Zap
  3. ABC Zap jane
  4. ABC Zap ABC
  5. Zap Zap Zap

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).

Enter Hours: 45Enter Rate: 10Pay: 475.0

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.

Score
Grade
>0.9
A
>0.8
B
>0.7
C
>0.6
D
<=0.6
F
Program Execution:

Enter score: 0.95A

Enter score: perfectBad score

Enter score: 10.0Bad score

Enter score: 0.75C

Enter score: 0.5F

Run the program repeatedly to test the various different values for input.