同学满分代码,购物车。
同学代码,购物车。
#!/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
22222222222
1 import random 2 shopping_list = [] 3 shop_list = [] 4 add = [] 5 p = [] 6 condition = True 7 identity_input = input( 8 "1. 用户\n" 9 "2. 商家\n" 10 "请选择对应身份序号[1|2]:") 11 if str.isdigit(identity_input): 12 identity_input = int(identity_input) 13 if identity_input == 1: 14 money_input = input("欢迎用户访问,输入购物金额:") 15 if money_input.isdigit(): 16 money_input = int(money_input) 17 with open("shoplist.txt", encoding='utf-8') as file: 18 for list in file: 19 shop_list.append((list.strip().split()[0], list.strip().split()[1])) 20 while condition: 21 for index, i in enumerate(shop_list): 22 print((index+1), i) 23 choice = input("请输入选择的商品序号,选择完毕q退出:") 24 if choice.isdigit(): 25 choice = int(choice) 26 if choice <= len(shop_list) and choice >= 0: 27 p = shop_list[choice-1] 28 a = int(p[1]) 29 if a <= money_input: #买的起 30 shopping_list.append(p) 31 money_input -= a 32 print("增加了%s商品,你的钱剩余%s" % (p, money_input)) 33 else: 34 print("余额不足,请重新选择,按q退出") 35 else: 36 print ("商品%s不存在" % choice) 37 elif choice == 'q': 38 print("--------已购商品列表--------") 39 for w in shopping_list: 40 print(w) 41 print("余额为%s" % money_input) 42 exit() 43 elif identity_input == 2: 44 print("欢迎商家访问:") 45 with open("shoplist.txt", encoding='utf-8') as file: 46 for list in file: 47 shop_list.append((list.strip().split()[0], list.strip().split()[1])) 48 while True: 49 for index, i in enumerate(shop_list): 50 print((index+1), i) 51 business_input = input( 52 "1.删除\n" 53 "2.添加\n" 54 "按q退出\n" 55 "请选择删除还是添加1|2:") 56 if str.isdigit(business_input): 57 business_input = int(business_input) 58 if business_input == 1: 59 business_choice = int(input("请输入要删除的商品序号:")) 60 del shop_list[business_choice-1] 61 c = 0 62 with open('shoplist.txt', 'w', encoding='utf-8') as d: 63 while c < len(shop_list): 64 d.write(shop_list[c][0]) 65 d.write(' ') 66 d.write(shop_list[c][1]) 67 d.write('\n') 68 c += 1 69 elif business_input == 2: 70 business_choice1 = input("请输入要添加的商品名称:") 71 business_choice2 = input("请输入要添加的商品价格:") 72 add.append(business_choice1) 73 add.append(business_choice2) 74 shop_list.append(add[-2:]) 75 with open('shoplist.txt', 'a', encoding='utf-8') as f: 76 f.write(business_choice1) 77 f.write(' ') 78 f.write(business_choice2) 79 f.write('\n') 80 else: 81 print("请输入正确的序号!!!!!!!!!!!!!!!!!!!!!!") 82 elif business_input == 'q': 83 print("正在退出。。。 。。。") 84 exit() 85 else: 86 print("身份输入错误,程序退出。。。 。。。") 87 else: 88 print("输入错误,没有输入的身份序号。")
记录Python学习过程,给自己改变的机会。