草稿,明天回公司写 #!/usr/bin/env python goods = {'IPhone7':6000,'Mate8':5000,'Thinkpad':3000,'Mac':'8000'} hea = {'商品信息':'价格'} msg = ' Welcome to JD ' msg_we = msg.center(100,'#') money = 10000 shoplist = [] buyshoplist = [] shopmoneylist = [] print(msg_we) print("您账户余额为:%s" %money) for k1,v1 in hea.items(): print(k1,v1) for k,v in goods.items(): print(k,v) shoplist.append(k) print("购物车内的商品为",buyshoplist) while True: shoplist1 = input("输入买的商品:") if shoplist1 in shoplist: print('您已成功购买商品',shoplist1) buyshoplist.append(shoplist1) print("购物车内的商品为",buyshoplist) shopmoney = int(goods[shoplist1]) shopmoneylist.append(shopmoney) print("价格总额为:",sum(shopmoneylist)) # print("价格总数为:",buymoneylist) else: choise = input("输入错误,是否结账退出(Y)es 结账退出,(N)o返回继续购买") if choise.capitalize() =='Y': if sum(shopmoneylist) > money: c = input("余额不足,充值(Y)es,减少商品(R)emove") if c.capitalize() == 'Y': m = int(input("输入充值金额:")) money += m print(money) if c.capitalize() == "R": pass else: break # print(choise) # if choise.capitalize() == 'Y': # continue
购物车功能几乎实现了贴出 第二个修改版,环境Centos python3
按照流程图写,最后写着写着跑偏了一点,流程图如下
1 [[email protected] python3]# vim shopping2.py 2 3 #!/usr/bin/env python 4 #author:lihongxing 5 #功能:在Python3中对购物车的实现,购买商品,结账金额大于帐户余额则提示充值或者减少商品,最后结算 6 #日期:2016/1/26 7 8 #导入sys模块 9 import sys 10 11 #定义变量信息 12 goods = {'IPhone7':6000,'Mate8':5000,'Thinkpad':3000,'Mac':'8000'} 13 hea = {'商品信息':'价格'} 14 msg = ' Welcome to JD ' 15 msg_we = msg.center(60,'#') 16 money = 10000 17 loop = True 18 19 #调试时出错,于是定义了几个空列表 20 shoplist = [] 21 buyshoplist = [] 22 buyremovlist = [] 23 shopmoneylist = [] 24 25 #显示帐户余额欢迎信息 26 print(msg_we) 27 print("您账户余额为:%s" %money) 28 for k1,v1 in hea.items(): 29 print(k1,v1) 30 31 ''' 32 把goods的字典内商品信息和价格for循环出然后增加到列表展示 33 提示:如果字典内的信息不做列表处理,下面while循环if判断时会有问题(结果会时对时错) 34 于是在上面把shoplist定义成了一个空列表 35 ''' 36 for k,v in goods.items(): 37 print(k,v) 38 shoplist.append(k) 39 print("购物车内的商品为",buyshoplist) 40 print(msg_we) 41 42 43 ''' 44 while循环主程序,输入商品 45 如果输入的商品在shoplist内则打印出,并把输入的信息增加到buyshoplist内 46 把shoplist的key对应的value取出,并转成整型,依次把取整后的value添加到shopmoneylist中 47 计算总价格赋值到allmoney 48 如果输入Q||q则退出程序 49 如果输入的商品不再shoplist内则判断输入是否为Y||y 50 如果为y则判断allmoney是否大于账户余额 51 ''' 52 while loop: 53 shoplist1 = input("输入要买的商品(按Q退出)按任意键结账:") 54 if shoplist1 in shoplist: 55 print('您已成功购买商品',shoplist1) 56 buyshoplist.append(shoplist1) 57 print("购物车内商品为",buyshoplist) 58 shopmoney = int(goods[shoplist1]) 59 shopmoneylist.append(shopmoney) 60 allmoney = sum(shopmoneylist) 61 print("价格1总额为:",allmoney) 62 elif shoplist1.capitalize() == 'Q': 63 sys.exit() 64 else: 65 choise = input("输入错误,是否结账退出(Y)es 结账退出,(N)o返回继续购买") 66 if choise.capitalize() =='Y': 67 if allmoney > money: 68 c = input("余额不足,充值(Y)es,减少商品(R)emove") 69 if c.capitalize() == 'Y': 70 m = int(input("输入充值金额:")) 71 money += m 72 print(money) 73 if c.capitalize() == "R": 74 allmoney = 0 #把购买总金额重新赋值为0,用于计算移除商>品后的总价格 75 shopmoney = 0 76 shopmoneylist = [] 77 print('购物车2内的商品为:' , buyshoplist) 78 removeshop = input("请输入要减掉的商品名称:") 79 buyshoplist.remove(removeshop) 80 print(buyshoplist) 81 for i in buyshoplist: 82 shopmoney = int(goods[i]) 83 shopmoneylist.append(shopmoney) 84 allmoney = sum(shopmoneylist) 85 86 print("价格2总额为:",allmoney) 87 else: 88 print("您已消费",allmoney, '账户余额为',money - allmoney) 89 break
以下为运行结果,更多人性化的设计还需修改
第三次修改,添加了第一天的登录验证功能,没有太多的改变,大致上就是把第一天的登录代码粘了进去。实践并明白了如何循环套循环在退出 break和continue的玩法,添加了flag但是好像不管用,就注释掉了,于是多用了几个break。如果不懂可以注释几个break运行一下便可
代码如下:
1 #!/usr/bin/env python 2 import sys 3 4 name = input("Please Input your name:") 5 passwd = "123" 6 count = 0 7 8 lock = open('lock.txt','r+') 9 lockname = lock.readlines() 10 11 12 13 goods = {'IPhone7':6000,'Mate8':5000,'Thinkpad':3000,'Mac':'8000'} 14 hea = {'商品信息':'价格'} 15 msg = ' Welcome to JD ' 16 msg_we = msg.center(60,'#') 17 money = 10000 18 loop = True 19 20 shoplist = [] 21 buyshoplist = [] 22 buyremovlist = [] 23 shopmoneylist = [] 24 25 26 27 for locknamelines in lockname: 28 locknamelines = locknamelines.strip('\n') 29 if name == locknamelines: 30 print("Account Be Locked!!Please Change User!") 31 quit() 32 lock.close() 33 34 #boss = True 35 36 while True: 37 while count <3: 38 userpasswd = input("Please Input your passwd:") 39 if userpasswd == passwd: 40 print(msg_we) 41 print("您账户余额为:%s" %money) 42 for k1,v1 in hea.items(): 43 print(k1,v1) 44 for k,v in goods.items(): 45 print(k,v) 46 shoplist.append(k) 47 print("购物车内的商品为",buyshoplist) 48 print(msg_we) 49 break 50 51 else: 52 print("Login falied,Please again ,Try", 3-count, 'times') 53 count += 1 54 else: 55 56 lockinput = open('lock.txt','a') 57 lockinputlist = lockinput.write(name + '\n') 58 print("Name Be Locked,Please Change User") 59 lockinput.close() 60 quit() 61 while loop: 62 shoplist1 = input("输入要买的商品(按Q退出)按任意键结账:") 63 if shoplist1 in shoplist: 64 print('您已成功购买商品',shoplist1) 65 buyshoplist.append(shoplist1) 66 print("购物车内商品为",buyshoplist) 67 shopmoney = int(goods[shoplist1]) 68 shopmoneylist.append(shopmoney) 69 allmoney = sum(shopmoneylist) 70 print("价格1总额为:",allmoney) 71 elif shoplist1.capitalize() == 'Q': 72 sys.exit() 73 else: 74 choise = input("输入错误,是否结账退出(Y)es 结账退出,(N)o返回继续购买") 75 if choise.capitalize() =='Y': 76 if allmoney > money: 77 c = input("余额不足,充值(Y)es,减少商品(R)emove") 78 if c.capitalize() == 'Y': 79 m = int(input("输入充值金额:")) 80 money += m 81 print(money) 82 if c.capitalize() == "R": 83 allmoney = 0 84 shopmoney = 0 85 shopmoneylist = [] 86 print('购物车2内的商品为:' , buyshoplist) 87 removeshop = input("请输入要减掉的商品名称:") 88 buyshoplist.remove(removeshop) 89 print(buyshoplist) 90 for i in buyshoplist: 91 shopmoney = int(goods[i]) 92 shopmoneylist.append(shopmoney) 93 allmoney = sum(shopmoneylist) 94 95 print("价格2总额为:",allmoney) 96 else: 97 print("您已消费",allmoney, '账户余额为',money - allmoney) 98 loop = False 99 # boss = False 100 break 101 102 103 104 105 106 107