标签:
同学代码,购物车。
#!/usr/bin/env python __author__ = ‘liudong‘ #定义空的购物列表: cart = [] #查询用户余额 import os,sys def money(): filename = sys.path[0]+‘/money.txt‘ if os.path.exists(filename): m = open(‘./money.txt‘,‘r‘) mon = m.readline() m.close() mon = int(mon) return mon else: exitt = False while not exitt: aa = input("\033[31m请输入你的工资: \033[0m").strip() if aa.isdigit(): mon = int(aa) exitt = True else: print ("请输入正确的数字。。") return mon #保存用户输入的余额 def free_money(x): a = open(‘./money.txt‘,‘w‘) a.write(‘%s‘%x) a.close() #定义余额变量 wage = money() wage1 = wage #定义总余额 shoping_money3 = 0 #定义用户总共消费 #定义商品列表: shop = [ ["Condoms",100], ["pill",99], ["girlfriend",1], ["mac book",8888], ["apple",5], ["乐视1S",1099] ] #循环显示购物信息: while True: #打印出商品列表和价格: print(‘\33[32m欢迎光临\33[37m‘.center(50,‘-‘)) print(‘\33[32m商品列表\33[37m‘.center(50,‘-‘)) for index,p in enumerate(shop): print (index,p[0],p[1]) #获取用户输入: shoping = input("请输入商品编号,结算请输入\33[32mall\33[37m,显示购物车请输入:\33[32mlist\33[37m,显示余额请输入:\33[32mfree\33[37m : ").strip() #判断用户输入是否合法 if shoping.isdigit(): shoping = int(shoping) if len(shop) < shoping: print (‘\33[31m你输入的产品号不存在,请重新选择商品号!\33[37m‘) continue shoping_money = shop[shoping][1] if shoping_money < wage: #判断用户是否可以购买 while True: num = input("请输入购买数量: ").strip() if not num.isdigit(): print ("\033[31m请正确输入商品数量。。\033[0m") else: num = int(num) if num == 0: break else: shoping_money2 = shoping_money * num #判断商品数量的价钱 if shoping_money2 < wage: #判断用户是否可以购买 cart.append(shop[shoping][0]) #把商品名称添加到购物车 cart.append(num) #把商品数量添加到购物车 print ("\033[31m您的商品已经加入购物车 %s 数量:%s \033[0m"%(shop[shoping][0],num)) wage = wage - shoping_money2 #算出用户剩余金额 shoping_money3 +=shoping_money2 #算出用户总消费金额 break else: print ("\033[31m你的工资买不起%s个%s \033[0m"%(num,shop[shoping][0])) break elif shoping_money > wage: #商品不能购买的提示 print ("\033[31m你的工资还买不起此商品: %s\033[0m" %shop[shoping][0]) else: #商品不能购买的提示 print ("\033[31m你的工资太少了,还是去努力赚钱吧。。\033[0m") break elif shoping == "list": #显示购物列表 print ("\033[31m----------shopping list----------\033[0m") num = 0 aa = len(cart) bb = int(aa / 2) for x in range(bb): print (cart[num],"\033[31m数量:\033[0m",cart[num+1]) num+=2 print ("\033[31m---------------------------------\033[0m") elif shoping == "free": #显示剩余金额 if not len(cart): print ("\033[31m你还没有购物,剩余:%s\033[0m" %wage) else: print ("\033[31m你一共花费:%s,剩余:%s\033[0m"%(shoping_money3,wage)) elif shoping == "all": #用户结算 if not len(cart): #判断购物车是否为空 print ("\033[31m你的购物车为空,不需要结算。。\033[0m") free_money(wage) break else: #购物车不为空,显示购物列表和购买商品数量 print ("\033[31m----------shopping list----------\033[0m") num = 0 aa = len(cart) bb = int(aa / 2) for x in range(bb): print (cart[num],"\033[31m数量:\033[0m",cart[num+1]) num+=2 print ("\033[31m---------------------------------\033[0m") gm = input("\033[31m是否确认购买:Y or N \033[0m ").strip() #判断用户是否购买商品 if gm == "N" or gm == "n": #用户不购买 print ("你没有购买任何商品,剩余:\033[31m%s\033[0m"%wage1) free_money(wage1) break elif gm == "y" or gm == "Y": #用户购买 print ("你一共花费\033[31m%s\033[0m还剩下\033[31m%s\033[0m"%(shoping_money3,wage)) free_money(wage) break else: #错误输入,默认用户不购买 print ("你输入的不正确,没有购买任何商品,剩余:\033[31m%s\033[0m"%wage1) free_money(wage1) break
标签:
原文地址:http://www.cnblogs.com/dcc001/p/5743407.html