__author__ = 'bruce'
importrandom
goodslist=[["ThinkPadE450",4199],["Macbook",12898],["IPhone6s",7788],["Surface",11088],["CanonEOS70D",6999],["Kindle",938],]
shopcart= [] #初始购物车
str1 = "价目表"str2= "物品(名)"str3= "价格(¥)"str4= "件数(个)"str5= "购物车清单"
def register():#注册模块
print("----------欢迎注册购物商场会员-----------")whileTrue:
name= input("请输入您需要注册的用户名:").strip()
with open('goldmenber.txt') as g:
goldname=g.read()if name ingoldname:print(("抱歉,用户名%s已经被注册") %format(name))continue
else:
g1= open('goldmenber.txt','a')
g1.write(name)
g1.write('\n')
g1.close()
print(("恭喜您,用户名%s注册成功!")%format(name))print("----------------------------------------")break
def shoppingcart(money):#已购物品清单模块
allcost =0print(str5.center(40,'-'))print(str2.ljust(10),str3.ljust(10),str4.ljust(15)) #左对齐输入物品名、价格、件数
for goods,price inshopcart:print(goods.ljust(15),str(price).ljust(15),str(1).ljust(15)) #遍历购物车,打印购物车明细
allcost +=priceprint(("您已经花费\033[44;36m¥%d\033[0m,还剩\033[41;36m¥%d\033[0m(按q退出购物,p打印当前已购商品)") %(allcost,money))print('-'*45)def shopmall():#购物模块
money = 20000
whileTrue:print(str1.center(42,'-'))for goods,prices in enumerate(goodslist): #遍历商品清单
print(goods,prices[0].ljust(30),'¥',prices[1])
goodselect= input(("您现在有\033[41;36m¥%d\033[0m,请选择需购买的商品编号(按q退出购物,p打印当前已购商品):") %money).strip()if goodselect.isdigit() and (int(goodselect) < len(goodslist)): #输入项为数值,且范围小于商品清单长度
goodselect =int(goodselect)
cost= int(goodslist[goodselect][1])if money >cost:
money-= cost #第一次购物之后余额
shopcart.append([goodslist[goodselect][0],goodslist[goodselect][1]]) #写入至购物车列表
else:print("您的余额不足,您可以选择充值")print(str5.center(40,'-'))print(str2.ljust(10),str3.ljust(10),str4.ljust(15)) #左对齐输入物品名、价格、件数
for goods,price inshopcart:print(goods.ljust(15),str(price).ljust(15),str(1).ljust(15)) #遍历购物车,打印购物车明细
print('-'*44)
rechargeselect= input("温馨提示:系统限制一次只能充值¥1000,选择充值(y/n):").strip()if rechargeselect == 'y':print("充值中,已为您充值\033[44;36m¥1000\033[0m")
money+=1000
elif rechargeselect == 'n':print(("您还剩\033[41;36m¥%d\033[0m,请继续购物") %money)
else:print("你的输入有误,回到购物界面")
elif goodselect == "q":
shoppingcart(money)print(("尊敬的金会员%s,欢迎您下次再来") %format(name))break
elif goodselect == "p":
shoppingcart(money)
else:print("您输入的编号有误,请重新选择(按q退出购物,p打印当前已购商品)")
if __name__ == '__main__':print("-"*40)print('***新年大酬宾,注册即送20000元购物卡***')print("-"*40)print("-----------欢迎来到购物商场------------")
name= input("输入您的会员卡号:").strip()
with open('goldmenber.txt') as g:
goldname=g.read()if name ingoldname:print(("尊敬的金会员%s,您获得20000元的购物卡") %format(name))else:print("对不起,本商城只对会员开放,请先注册会员")
register()
shopmall()