利用def,建立一個「交換兩數」功能的函數,重新設計4-1節的實例演練,即交換星期日及星期六的動物園遊客人數。
def swap(a, i, j):
temp = a[i]
a[i] = a[j]
a[j] = temp # 不必return
num_visitors = [70, 10, 14, 7, 25, 30, 50] # 索引0~6代表星期日到六
print(num_visitors)
swap(num_visitors, 0, 6) # 把星期日及星期六的人數交換
print(num_visitors)
"[70, 10, 14, 7, 25, 30, 50] [50, 10, 14, 7, 25, 30, 70]"
| ID | User | Problem | Subject | Hit | Post Date |
沒有發現任何「解題報告」 |
|||||