撰寫一個純函式
來寫你的第一個純函式吧!你手上的資料包含公司庫存所有品項的價格資訊。不過你一開始沒注意到,這些價格其實是以「分」為單位,而你原本以為是「元」。你需要撰寫一個函式,把所有價格從分換算成元,也就是將數值除以 100。請務必避免產生任何副作用,且不要依賴函式外部的值!
本練習屬於課程
Programming Paradigm 概念
練習說明
- 完成下方程式碼,撰寫一個純函式,將清單(
input_list)作為輸入,把清單中每個元素除以 100,然後回傳新的清單。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
def square_list(input_list):
new_list = []
# Loop through each item in the input list
for item in ____:
# Divide each item by 100
new_item = ____ / 100
# Append the new item to the new list
new_list.append(____)
# Return the new list
return ____