知识点: 文件读,写操作,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中文网其他相关文章!

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

本文解释了如何使用美丽的汤库来解析html。 它详细介绍了常见方法,例如find(),find_all(),select()和get_text(),以用于数据提取,处理不同的HTML结构和错误以及替代方案(SEL)

本文比较了Tensorflow和Pytorch的深度学习。 它详细介绍了所涉及的步骤:数据准备,模型构建,培训,评估和部署。 框架之间的关键差异,特别是关于计算刻度的

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

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

本文指导Python开发人员构建命令行界面(CLIS)。 它使用Typer,Click和ArgParse等库详细介绍,强调输入/输出处理,并促进用户友好的设计模式,以提高CLI可用性。

文章讨论了虚拟环境在Python中的作用,重点是管理项目依赖性并避免冲突。它详细介绍了他们在改善项目管理和减少依赖问题方面的创建,激活和利益。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

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

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

Atom编辑器mac版下载
最流行的的开源编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。