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:9000 0 ‘apple‘ 7000 ni xianng mai shen me?0 ..... 1 ‘watch‘ 3000 ...
在编写的过程中要注意思路清晰,一步一步来,可以在纸上列下流程图,多加练习。
杀人须就咽喉着刀,吾辈为学,当从心髓入微处用力,自然笃实光辉。