標籤:item for enum day bsp 下一步 idt cti exit
production = [ ["Apple",7000], ["watch",3000], ["pad",3500], ["book",100],]shopping_list =[]salary = input("Your salary:")if salary.isdigit(): salary = int(salary) while True: for index,item in enumerate(production): #print(production.index(item),item) print(index,item) choice = input("你想買什嗎?") if choice.isdigit(): choice = int(choice) if choice < len(production) and choice >= 0: if production[choice][1] <= salary: salary -= production[choice][1] shopping_list.append(production[choice]) print("你已經購買了%s,還剩下\033[41;1m%s\033[0m"%(production[choice][0],salary)) else: print("你的錢不夠") else: print("你要購買的商品不存在") elif choice =="q": print("--------shopping list----------") for p in shopping_list: print(p) print("你的錢還剩下",salary) exit() else: print("請重新輸入商品編碼")else: print("請重新輸入")
備忘:注意for迴圈的使用,比如第一個for迴圈處,print完下一步choice應該與for處於同一層次,才能列印出完整的商品列表,否則將會如下
Your salary:90000 ‘apple‘ 7000ni xianng mai shen me?0.....1 ‘watch‘ 3000...
在編寫的過程中要注意思路清晰,一步一步來,可以在紙上列下流程圖,多加練習。
殺人須就咽喉著刀,吾輩為學,當從心髓入微處用力,自然篤實光輝。
python學習 day2購物車程式