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 购物车----之用户部分的更多相关文章
- python 购物车小程序
python 购物车小程序 功能要求:1.启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表2.允许用户根据商品编号购买商品3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4. ...
- python购物车-基础版本
# 1. 用户先给自己的账户充钱:比如先充3000元.# 2. 页面显示 序号 + 商品名称 + 商品价格,如:# 1 电脑 1999# 2 鼠标 10# …# n 购物车结算# 3. 用户输入选择的 ...
- 简单的python购物车
这几天,一直在学python,跟着视频老师做了一个比较简单的python购物车,感觉不错,分享一下 products = [['Iphone8',6888],['MacPro ...
- python入门:用户登录,三次错误机会
#!/usr/bin/env python # -*- coding:utf-8 -*- #用户登录,三次机会重试 #主要分为两个部分,一部分是写三次循环,一部分写用户输入 #用户登录的实现,循环3次 ...
- python初步编写用户登录
python初步编写用户登录 python编写用户登录 用python写一个脚本,使得这个脚本在执行后,可以产生如下的效果: 1.用户的账号为:root 密码为:westos 2.用户账号和密码均输 ...
- Python之读取用户指令和格式化打印
Python之读取用户指令和格式化打印 一.读取用户指令 当你的程序要接收用户输入的指令时,可以用input函数: name = input("请输入你的名字:") print(& ...
- python购物车小案例
python购物车小案例# 案列描述:有一个小型水果店里面有水果(苹果:¥8/kg,香蕉:¥5/kg,芒果:¥15/kg,葡萄:¥12/kg),客户带了100元钱进店选购水果.# 1.客户输入相应序号 ...
- python小练习:用户三次登陆, 购物车
2018.12.1 周末练习: 1.用户三次登陆 from random import randint i = 1 while i < 4: num = 0 verify_code = '' w ...
- python 购物车+用户认证程序
创建文件a.txt,b.txt.c.txt用于存放应该持续保存的信息 a.txt :用户密码输入错误3次就锁定 b.txt :购物时的活动,每个用户只能参与一次 c:txt :购物完后的发票在这里查看 ...
随机推荐
- Util应用程序框架公共操作类
随笔分类 - Util应用程序框架公共操作类 Util应用程序框架公共操作类 Util应用程序框架公共操作类(五):异常公共操作类 摘要: 任何系统都需要处理错误,本文介绍的异常公共操作类,用于对业务 ...
- 在Ubuntu中编译QT工程Tesful
今天晚上开机到Ubuntu中了,试了一下之前在Windows下建立的Tesful工程,发现没有任何改动就可以编译成功/运行. 附上图:
- 在项目管理工具Redmine中使用SubVersion进行版本管理
原文:在项目管理工具Redmine中使用SubVersion进行版本管理 在项目管理工具Redmine中使用SubVersion进行版本管理 分类: Redmine2009-06-01 10:11 5 ...
- 【转】Android的Merge讲解与实例
原文:http://blog.sina.com.cn/s/blog_62f987620100sf13.html 单独将<merge />标签做个介绍,是因为它在优化UI结构时起到很重要的作 ...
- Entity Framework,TransactionScope 混合使用的问题讨论
using (var ts = new TransactionScope()) { string connStr = "Data Source=.;Initial Catalog=Test; ...
- 微信SDK使用总结
最近做一个分享功能,需要使用微信SDK分享内容给朋友或朋友圈.期间遇到些奇怪的问题,花了点时间去折腾了一番. 首先需要到微信开放平台http://open.weixin.qq.com/?lang=zh ...
- 关于ADO.NET参数化查询的提问
最近我们的正式环境一直在报错一个异常,首先我贴出来异常信息 BLL层 捕获到了请求的url Net.BLL.MobileFun MobileFun.GetBusinessBidPolicy 异常 传入 ...
- SOLID 设计原则 In C# 代码实现
[S] Single Responsibility Principle (单一职责原则) 认为一个对象应该仅只有一个单一的职责 namespace SingleResponsibilityPrinci ...
- 获得WCF Client端的本地端口
获得WCF Client端的本地端口 最近需要做个小功能,当WCF调用远程服务时,显示该调用的网速或流量.其中比较关键的一步就是需要获得WCF Client端的本地端口,原来以为是个简单的事情,结果 ...
- NET那点不为人知的事
ASP.NET那点不为人知的事(一) 我们上网时,在浏览器地址输入网址:Http://www.cnblogs.com,按下回车,一张网页就呈现在我们眼前.这究竟发生了什么?对于一名优秀的Progr ...