一个简单的购物车程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个简单的购物车程序相关的知识,希望对你有一定的参考价值。
前几天跟着视频做了一个简单的购物车程序,但是没有做出来,之后又跟着后面的视频,做了一个,视频里面老师也讲了,只有不断的尝试写,才能慢慢有感觉,越不写,越不会。下面是我前几天写的购物车和跟着视频做的购物车的对比:
自己写的:
1 __Author__ = "Zhang Peng" 2 3 salary = input("What is your monthly salary?\n") 4 somethings={"IPhone":5888,"杯子":8,"眼镜盒":15,"计算器":38,"鼠标":128,"电脑":7889,"零食":120,} 5 Shopping_Cart=[] 6 7 while True: 8 dongxi = input("What do you want to buy? \n") 9 10 if dongxi == ‘quit‘: 11 break 12 13 elif somethings[dongxi] == ‘KeyError‘: 14 print("Sorry!The product you want is not found.") 15 break 16 17 elif salary - somethings[dongxi] >= 0: 18 Shopping_Cart.append([dongxi,somethings[dongxi]],) 19 salary = salary - somethings[dongxi] 20 21 22 elif salary - somethings[dongxi] < 0: 23 print("The balance you have left is not enough to buy it. Try something else, please.") 24 print("Enter ‘quit‘ to end the program.") 25 26 27 28 29 print(f‘你还有{salary}块钱!‘) 30 print(f‘Your shopping list:\n{Shopping_Cart}‘)
老师写的:
product_list = [ (‘Iphone‘,5800), (‘Mac Pro‘,9800), (‘Bike‘,800), (‘Watch‘,10600), (‘Coffee‘,31), (‘Alex Python‘,120), ] shopping_list = [] salary = input("Input your salary:") if salary.isdigit(): salary = int(salary) while True: for index,item in enumerate(product_list): #print(product_list.index(item),item) print(index,item) user_choice = input("选择要买嘛?>>>:") if user_choice.isdigit(): user_choice = int(user_choice) if user_choice < len(product_list) and user_choice >=0: p_item = product_list[user_choice] if p_item[1] <= salary: #买的起 shopping_list.append(p_item) salary -= p_item[1] print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" %(p_item,salary) ) else: print("\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[0m" % salary) else: print("product code [%s] is not exist!"% user_choice) elif user_choice == ‘q‘: print("--------shopping list------") for p in shopping_list: print(p) print("Your current balance:",salary) exit() else: print("invalid option")
? 后面的视频布置的作业是把购物车进行优化,优化的内容是,分别添加用户和商家入口,用户可以显示自己以前的购物记录,已经钱包余额;商家可以添加商品和修改商品价格,下面代码是我自己研究研究写出来的,后面的保存和调用的方法还是不熟练,写的比较乱,试了好多遍,还是不行,大家可以看看:
product_list=[ (‘Iphone‘,5888), (‘笔记本‘,12888), (‘手表‘,10600), (‘电视‘,3888), (‘书‘,58), ] id_list=["商家","用户","shangjia","yonghu"] id_shopping=input("Please input your ID:\n") exit_flag=False if id_shopping=="shangjia": choice=input("你是否想添加\修改商品?添加商品输入‘1‘,修改商品输入‘2‘,退出输入‘q‘:\n") if choice=="1": while not exit_flag: name=input("请输入您想添加的商品名称:\n") name2=input("请输入您添加商品的价格:\n") if name=="q" or name2=="q": exit_flag=True else: product_list.append((name,name2),) elif choice=="2": while not exit_flag: name3=input("请输入您想修改价格的商品名称:\n") name4=input("请输入您想要修改的价格:\n") if name3=="q" or name4=="q": exit_flag=True else: a=product_list.index(name3) product_list.remove(name3) product_list.index((name3,name4)) else: if choice=="q": exit_flag=True print(product_list) elif id_shopping=="yonghu": shopping_list=[] with open("shopping_list.txt") as f: print("你的购物车里已经有:\n") f.read() with open("salary.txt") as f: salary=f.read() print(salary) if salary.isdigit(): #isdigit 是判断输入的是不是一个数字 salary=int(salary) while True: print("我们有一下商品:\n") for index,item in enumerate(product_list): print(index,item) user_choice=input("请问你还想买什么?:\n") if user_choice.isdigit(): user_choice=int(user_choice) if user_choice<len(product_list) and user_choice>0: p_item=product_list[user_choice] if p_item[1]<salary: #买不起 shopping_list.append(p_item) salary -=p_item[1] print("您买的商品已经加入购物车") else: print("没钱别嘚瑟了") else: print("没有您要的商品") elif user_choice == ‘q‘: print("----------shopping list----------") for p in shopping_list: print(p) with open("shopping_list.txt","w") as f: f.write("shopping_list") print("你还剩余",salary) salary.save("salary.txt") else: print("输入错误") else: shopping_list=[] salary=input("请输入你的月薪:\n") if salary.isdigit(): #isdigit 是判断输入的是不是一个数字 salary=int(salary) while True: for index,item in enumerate(product_list): print(index,item) user_choice=input("请问你想买什么?:\n") if user_choice.isdigit(): user_choice=int(user_choice) if user_choice<len(product_list) and user_choice>0: p_item=product_list[user_choice] if p_item[1]<salary: #买不起 shopping_list.append(p_item) salary -=p_item[1] print("您买的商品已经加入购物车") else: print("没钱别嘚瑟了") else: print("没有您要的商品") elif user_choice == ‘q‘: print("----------shopping list----------") for p in shopping_list: print(p) shopping_list.save("shopping_list.txt") print("你还剩余",salary) with open("salary.txt","w") as f: f.write("salary") exit() else: print("输入错误")
有兴趣的可以看一下,欢迎踊跃参与。给我写建议,谢谢!
以上是关于一个简单的购物车程序的主要内容,如果未能解决你的问题,请参考以下文章