python购物车程序简单代码
本文实例为大家分享了python购物车程序的具体代码,供大家参考,具体内容如下
代码:
''''' Created on 2017年9月4日 @author: len ''' product_list = [ ('Robot',200000), ('MacPro',12000), ('Iphone8',8888), ('Hello World',1200), ] shopping_list = [] user_salary=input("请输入你的工资:") if user_salary.isdigit(): user_salary = int(user_salary) while True: print("商品如下:") for index,item in enumerate(product_list): print (index,item) user_choice = input("请输入要购买的商品编号:") if user_choice.isdigit(): user_choice = int(user_choice) if user_choice < len(product_list) and user_choice > -1: p_item = product_list[user_choice] if user_salary>=p_item[1]: shopping_list.append(p_item) user_salary-=p_item[1] print("购买商品",p_item,"成功您的余额为",user_salary,"元!" ) else: print("您的余额为",user_salary,"余额不足以购买此商品,购买失败!") else: print("并无此产品!") elif user_choice == "q": print("--------shopping list-------") for i in shopping_list: print(i) exit() else: print("invalidate!!!")
效果图:
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16