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 :购物完后的发票在这里查看 ...
随机推荐
- jQuery小例
jQuery小例子 使用前,请先引用jquery 1,map遍历数组 2,jQuery对象与DOM对象才做元素和互转 3,prevall与nextall 4,jquery版的星星评分控件 5,jq ...
- C#:判断当前程序是否通过管理员运行
原文:C#:判断当前程序是否通过管理员运行 public bool IsAdministrator() { WindowsIdentity current = WindowsIdentity.GetC ...
- 【转】【Android工具】被忽略的UI检视利器:Hierarchy Viewer
原文:http://blog.csdn.net/ddna/article/details/5527072 Hierarchy Viewer是随AndroidSDK发布的工具,位置在tools文件夹下, ...
- oledb快速导入Excel案例
DataTable dtImportExcel = null; string pathFile = Server.MapPath("~/ErrorCatory.xlsx"); // ...
- PushSharp的使用
PushSharp的使用 最近做公司的一个项目.一旦数据库插入新的消息,就要通知服务器,将这些新的消息推送给苹果客户端,以前我们的项目中有人做过这个功能,无奈做的有点复杂,而且代码没注释,我压根就没看 ...
- GLFW3出error adding symbols: DSO missing from command line解决
背景:使用OpenGL的GLFW3.1库的时候,使用其中一些代码 报error adding symbols: DSO missing from command line 因为使用的是Qcreator ...
- Hibernate的三种缓存
一级缓存 hibernate的一级缓存是跟session绑定的,那么一级缓存的生命周期与session的生命周期一致,因此,一级缓存也叫session级缓存或者事务级缓存. 支持一级缓存的方法有: q ...
- javaFile循环列出指定目录下的所有文件(源代码)
package javatest.basic22; import java.io.File; import java.io.IOException; public class FileTest { p ...
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Day1:T1 模拟 T2 拓扑排序
T1:模拟 自己第一天的简直跟白痴一样啊...模拟都会打错.. 当时貌似在更新最大值的时候打逗比了... if((sum[x]==max && x<maxh) || sum[x] ...