跳高比賽的成績(釐米)都在列表裡。寫 silver(heights),返回第二高的成績:和第一名一樣高的不算第二名。如果所有人都一樣高,或者列表是空的,返回 None。
silver([120, 135, 128])→128silver([140, 140, 130])→130silver([110, 110])→Nonedef silver(heights):
if len(heights) == 0:
return None
best = max(heights)
others = []
for h in heights:
# 在這裡寫:比 best 低的放進 others
pass
# 在這裡寫:others 是空的返回 None,不然返回其中最大的
return None
寫好了先點「執行」試試示例,沒問題再點「提交」。