要求:python
一、啓動程序後,輸入用戶名密碼後,讓用戶輸入工資,而後打印商品列表git
二、容許用戶根據商品編號購買商品 app
三、用戶選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒code
四、可隨時退出,退出時,打印已購買商品和餘額 blog
思路:input
一、index,和for循環商品列表it
二、商品號要在商品序列內io
三、判斷餘額for循環
四、打印所選商品及價格class
goods =[ {"name": "電腦", "price": 1999}, {"name": "鼠標", "price": 10}, {"name": "遊艇", "price": 20}, {"name": "美女", "price": 998}, ] shopping_list = [] wage = input('請輸入你的工資:') if wage.isdigit(): wage=int(wage) while True: for index,i in enumerate(goods): print(index,i) user_choice = input('請選擇要買的編號>>:') if user_choice.isdigit(): user_choice = int(user_choice) if user_choice < len(goods) and user_choice >=0: goods_item = goods[user_choice] print(goods_item['price']) if goods_item['price'] < wage:#買的起 shopping_list.append(goods_item) wage -=goods_item['price'] print("添加 %s 到你的購物車,你的餘額爲 \033[31;1m%s\033[0m" %(goods_item,wage) ) else: print("\033[41;1m你的餘額不足[%s]啦\033[0m" % wage) else: print("product code [%s] is not exist!"% user_choice) elif user_choice == 'q': print("--------shopping list------") for p in shopping_list: print(p) print("你的餘額爲:",wage) exit() else: print("invalid option")