知识点: 文件读,写操作,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 购物车----之用户部分的更多相关文章

  1. python 购物车小程序

    python 购物车小程序 功能要求:1.启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表2.允许用户根据商品编号购买商品3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4. ...

  2. python购物车-基础版本

    # 1. 用户先给自己的账户充钱:比如先充3000元.# 2. 页面显示 序号 + 商品名称 + 商品价格,如:# 1 电脑 1999# 2 鼠标 10# …# n 购物车结算# 3. 用户输入选择的 ...

  3. 简单的python购物车

                 这几天,一直在学python,跟着视频老师做了一个比较简单的python购物车,感觉不错,分享一下 products = [['Iphone8',6888],['MacPro ...

  4. python入门:用户登录,三次错误机会

    #!/usr/bin/env python # -*- coding:utf-8 -*- #用户登录,三次机会重试 #主要分为两个部分,一部分是写三次循环,一部分写用户输入 #用户登录的实现,循环3次 ...

  5. python初步编写用户登录

     python初步编写用户登录 python编写用户登录 用python写一个脚本,使得这个脚本在执行后,可以产生如下的效果: 1.用户的账号为:root 密码为:westos 2.用户账号和密码均输 ...

  6. Python之读取用户指令和格式化打印

    Python之读取用户指令和格式化打印 一.读取用户指令 当你的程序要接收用户输入的指令时,可以用input函数: name = input("请输入你的名字:") print(& ...

  7. python购物车小案例

    python购物车小案例# 案列描述:有一个小型水果店里面有水果(苹果:¥8/kg,香蕉:¥5/kg,芒果:¥15/kg,葡萄:¥12/kg),客户带了100元钱进店选购水果.# 1.客户输入相应序号 ...

  8. python小练习:用户三次登陆, 购物车

    2018.12.1 周末练习: 1.用户三次登陆 from random import randint i = 1 while i < 4: num = 0 verify_code = '' w ...

  9. python 购物车+用户认证程序

    创建文件a.txt,b.txt.c.txt用于存放应该持续保存的信息 a.txt :用户密码输入错误3次就锁定 b.txt :购物时的活动,每个用户只能参与一次 c:txt :购物完后的发票在这里查看 ...

随机推荐

  1. [译]Java 设计模式 之模板方法

    (文章翻译自Java Design Pattern: Template Method) 模板方法设计模式定义了归档特定操作的工作流.它允许子类去修改特定的步奏而不用改变工作流的结构. 下面的例子表示模 ...

  2. JTAG应该如何接线

    下面是某个ARM9评估板的原理图: 注意: 1. Vref和Vtarget可以直接连在一起,由被调试板提供3.3V或5V电源: 2. nTRST,最好上拉: 3. TDI,最好上拉 4. TMS,最好 ...

  3. javascript 学习总结(三)Boolean对象

    Boolean对象 /* 创建 Boolean 对象的语法: new Boolean(value); //构造函数 Boolean(value); //转换函数 参数 value 由布尔对象存放的值或 ...

  4. Web学习-apache视图log刊物

    视图apache刊物 apache日志位置 不同的系统位置不同. widnows 假如是windows的话,xampp下应该是都存在的,直接去找apache的folder/log/access.log ...

  5. 目前比较流行的Python科学计算发行版

    经常有身边的学友问到用什么Python发行版比较好? 其实目前比较流行的Python科学计算发行版,主要有这么几个: Python(x,y) GUI基于PyQt,曾经是功能最全也是最强大的,而且是Wi ...

  6. Nutch搜索引擎Solr简介及安装

    Nutch搜索引擎(第2期)_ Solr简介及安装   1.Solr简介 Solr是一个高性能,采用Java5开发,基于Lucene的全文搜索服务器.同时对其进行了扩展,提供了比Lucene更为丰富的 ...

  7. Linux普通文件和设备的异同点

    众所周知,Linux下一切皆文件,文件包含数据,具有属性,通过目录中的名字被标识,可以从一个文件读取数据,写入另一个文件,而Linux把这写应用于设备. 请看如下普通文件和设备的对比:

  8. Model Validation(模型验证)

    Model Validation(模型验证) 前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/344 ...

  9. c# 即使服务又是可执行程序的代码实现

    先看下代码 namespace UpdaterServer { class Program { static void Main(string[] args) { ) { ServiceBase[] ...

  10. CPU 硬盘性能

    CPU 硬盘性能到底相差多少 本文以一个现代的.实际的个人电脑为对象,分析其中CPU(Intel Core 2 Duo 3.0GHz)以及各类子系统的运行速度——延迟和数据吞吐量.通过粗略的估算PC各 ...