Python购物车之用户部分代码
知识点: 文件读,写操作,if 判断, for 循环
salary = input("输入你的工资:") bought_list = [] product_list = {} with open("product_list","r",encoding="utf-8") as f1: for item in f1: p_name,p_price = item.strip().split(':') product_list[p_name]=int(p_price) print(product_list) if salary.isdigit(): salary = int(salary) while True: user_choice = input("please input product which you want:") if user_choice in product_list.keys(): if product_list[user_choice] <= salary: bought_list.append(user_choice) #增加列表的元素 salary = salary - product_list[user_choice] print("Had bought [\033[32;1m%s\033[0m], and your balance is \033[31;1m%s\033[0m"%(user_choice,salary)) print(bought_list) else: print("Your balance is less than product's price") continue elif user_choice is 'q': with open('bought.txt','w+',encoding='utf-8') as f2: for goods in bought_list: print(goods,file=f2) exit(print("You had bought %s goods, and your balance is %s"%(bought_list, salary))) else: print("The good had been sold out")
登录后复制
测试:
输入你的工资:15000 {'Python': 20, 'Iphone': 5288, 'Iwatch': 3288, 'Bike': 2400, 'Mac pro': 12888} please input product which you want:Python Had bought [Python], and your balance is 14980 ['Python'] please input product which you want:Bike Had bought [Bike], and your balance is 12580 ['Python', 'Bike'] please input product which you want:Mac pro Your balance is less than product's price please input product which you want:Iwatch Had bought [Iwatch], and your balance is 9292 ['Python', 'Bike', 'Iwatch'] please input product which you want:q You had bought ['Python', 'Bike', 'Iwatch'] goods, and your balance is 9292
登录后复制
以上是Python购物车之用户部分代码的详细内容。更多信息请关注PHP中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信条阴影:贝壳谜语解决方案
3 周前
By DDD
Windows 11 KB5054979中的新功能以及如何解决更新问题
2 周前
By DDD
威尔R.E.P.O.有交叉游戏吗?
1 个月前
By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

本文讨论了诸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和请求等流行的Python库,并详细介绍了它们在科学计算,数据分析,可视化,机器学习,网络开发和H中的用途

在Python中,如何通过字符串动态创建对象并调用其方法?这是一个常见的编程需求,尤其在需要根据配置或运行...
