python之初级购物车小程序
作业需求:
思路与源码:

username = input("Username:") password = input("Password:") print(username) print(password) print("Welcome logging".center(50,'-')) save_user = open("save_username.txt", "w") #打开文件 save_user.write(username) #将文件用户密码保存 save_user.write(password) salary = input("Input your salary:") #输入salary if salary.isdigit(): #判断salary是否数字 salary = int(salary) else: print("Invalid data type...please input again.") exit() welcome_msg = "Welcome to Spping Mall".center(50,'-') print(welcome_msg) #输出欢迎界面 product_list = [ #商品列表,里面放元组(只读列表) ('Ipad',7000), ('Iphone',5400), ('Book',40), ('fruit',14.4), ('Car',150000), ('Bike',250) ] shopping_car = [] exit_flag = False while not exit_flag: print("Product list".center(50,'-')) for item in enumerate(product_list): #输出商品列表 index = item[0] #eg:item == (0, ('Ipad', 7000)) 商品序号,从0开始 prod_name = item[1][0] #重要 prod_price = item[1][1] print(index,':',prod_name,prod_price) user_choice = input("[q=quit,c=check]What do you want to buy?:") if user_choice.isdigit(): #输入的是数字,选商品 user_choice = int(user_choice) if 0 <= user_choice < len(product_list): #输出的数字不超界 p_item = product_list[user_choice] #选中的元组,eg:('Ipad',7000) count_of_prod = int(input("How many do you want to buy?:")) #输入购买商品数量 if p_item[1] * count_of_prod <= salary: #钱够 for i in range(count_of_prod): #放入购物车 shopping_car.append(p_item) salary -= p_item[1] * count_of_prod #扣钱 print("%s in your Shopping_car;and your current balance is %s" % (p_item ,salary)) else: #余额不足 print("your balence is %s;and You have no enough money to buy it!" % salary) else: print("The number go beyond.Try again...") else: #user_choice == 'q' or 'quit': 这样是错误的!!! if user_choice == 'q' or user_choice == 'quit': #输入q退出 print('Purchased product as below'.center(50, '*')) print(shopping_car) print('Your balance is %s' % salary) print('End'.center(50,'*')) exit_flag = True elif user_choice == 'c' or user_choice == 'check': #输入c检查,输出购物车列表 print('Purchased product as below'.center(50,'*')) for item in shopping_car: print(item) print('Your balance is %s' % salary) else: print("Please input the number of product...")
运行结果:

C:\Python34\python3.exe C:/Users/Administrator/PycharmProjects/laonanhai/day2/shopping优化.py Username:zcl Password:qwe zcl qwe -----------------Welcome logging------------------ Input your salary:1000 --------------Welcome to Spping Mall-------------- -------------------Product list------------------- 0 : Ipad 7000 1 : Iphone 5400 2 : Book 40 3 : fruit 14.4 4 : Car 150000 5 : Bike 250 [q=quit,c=check]What do you want to buy?:4 How many do you want to buy?:1 your balence is 1000;and You have no enough money to buy it! -------------------Product list------------------- 0 : Ipad 7000 1 : Iphone 5400 2 : Book 40 3 : fruit 14.4 4 : Car 150000 5 : Bike 250 [q=quit,c=check]What do you want to buy?:5 How many do you want to buy?:2 ('Bike', 250) in your Shopping_car;and your current balance is 500 -------------------Product list------------------- 0 : Ipad 7000 1 : Iphone 5400 2 : Book 40 3 : fruit 14.4 4 : Car 150000 5 : Bike 250 [q=quit,c=check]What do you want to buy?:2 How many do you want to buy?:2 ('Book', 40) in your Shopping_car;and your current balance is 420 -------------------Product list------------------- 0 : Ipad 7000 1 : Iphone 5400 2 : Book 40 3 : fruit 14.4 4 : Car 150000 5 : Bike 250 [q=quit,c=check]What do you want to buy?:c ************Purchased product as below************ ('Bike', 250) ('Bike', 250) ('Book', 40) ('Book', 40) Your balance is 420 -------------------Product list------------------- 0 : Ipad 7000 1 : Iphone 5400 2 : Book 40 3 : fruit 14.4 4 : Car 150000 5 : Bike 250 [q=quit,c=check]What do you want to buy?:q ************Purchased product as below************ [('Bike', 250), ('Bike', 250), ('Book', 40), ('Book', 40)] Your balance is 420 ***********************End************************ Process finished with exit code 0
流程图:
先分享下画在线流程图的网址https://www.processon.com/tour
作者:前程明亮
出处:http://www.cnblogs.com/0zcl
文章未标明转载则为原创博客。欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
出处:http://www.cnblogs.com/0zcl
文章未标明转载则为原创博客。欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
1.非系统的学习也是在浪费时间
2.做一个会欣赏美,懂艺术,会艺术的技术人
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?