DAY 7
2

練習1

• 如果a是負數或為0，回傳 “Result is a negative number.”
• 如果a是奇數，回傳 -(-a)**(1/2)
• 如果a是偶數，回傳 a**(1/2)
``````def square_root(a):
___(1)____
return 'Result is a negative number.'
___(2)____
___(3)___
return a ** (1/2)
else:
return -(-a)**(1/2)
``````

(1)
A. if a <= 0:
B. if a % 2 == 0:
C. else:
D. elif

(2)
A. if a <= 0:
B. if a % 2 == 0:
C. else:
D. elif

(3)
A. if a <= 0:
B. if a % 2 == 0:
C. else:
D. elif

練習2

• 80分(含)以上的人獲得 “A” 評級
• 60分(含)以上但低於80分的人獲得 “B” 評級
• 60分以下的人獲得 “C” 評級
``````def get_rating(grade):
if ___(1)___
elif ___(2)___
else: ___(3)___
return rating
``````

(1)
A. grade < 80: rating = 'B'
B. grade < 60: rating = 'C'
C. rating = ‘B'
D. rating = 'A'

(2)
A. grade < 80: rating = 'B'
B. grade < 60: rating = 'C'
C. rating = ‘B'
D. rating = 'A'

(3)
A. grade < 80: rating = 'B'
B. grade < 60: rating = 'C'
C. rating = ‘B'
D. rating = 'A'

練習3

• 如果 a 是負數，則回傳 “值是負數”
• 如果 a 不是負數，則為非負數，再繼續判別。
• 如果 a 大於 0 ，則回傳 “值是正數” ，否則回傳 “值是零”
``````def reResult(a):
___(1)___
___(2)___
___(3)___
___(4)___
``````

(1)
A. if a < 0:
B. if a > 0:
C. else:
D. elif:

(2)
A. if a < 0:
B. if a > 0:
C. else:
D. elif:

(3)
A. if a < 0:
B. if a > 0:
C. else:
D. elif:

(4)
A. if a < 0:
B. if a > 0:
C. else:
D. elif: