標籤:int 商品列表 bsp logs 啟動 end 清單 else python
# readme:# ne_zha# blogger address:www.cnblogs.com/ne-zha# 要求:# 1.啟動程式,讓使用者輸入存款,然後列印商品列表# 2.允許使用者根據商品編號選擇購買商品# 3.使用者選擇完商品後檢測餘額是否足夠扣款,滿足提示扣款成功,不滿足提示餘額不足# 4.可隨時退出,退出時列印購買商品和餘額savings =input(‘請輸入您的存款:‘)commodity_list =[(‘iphone7‘,5888),(‘bike‘,1500),(‘huaweiP9‘,3666),(‘headset‘,199)]if savings.isdigit(): savings =int(savings) while True: for item in enumerate(commodity_list): print(item) user_choice =input(‘請輸入您想要的商品編號:‘) if user_choice.isdigit(): user_choice =int(user_choice) if user_choice < len(commodity_list) and user_choice >= 0: choice = commodity_list[user_choice] price_choice =choice[1] if price_choice<=savings: buy_user =[] buy_user.append(choice[0]) print(‘商品\033[1;31;40m%s\033[0m已加入購物車‘%choice[0]) savings-=price_choice print(‘餘額:\033[1;31;40m%s\033[0m‘%savings) else: print(‘餘額不足,購買失敗!‘) else: print(‘商品輸入有誤,請重新輸入:‘) elif user_choice ==‘q‘: print(‘您已買的商品清單與餘額:‘) for i in buy_user: print(‘\033[1;31;40mi\033[0m) print(‘\033[1;31;40msavings\033[0m‘) exit() else: print(‘invalid option‘)
python日記,購物車程式