python用字典编写购物程序_Python编写购物小程序
购物车要求:
用户名和密码存放于文件中
启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
'''
用户名和密码存放于文件中
启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额
'''
product_dic = { 1:['Iphone7',5800],
2:['Coffee',30],
3:['疙瘩汤',10],
4:['Python Book',99],
5:['Bike',199],
6:['ViVo X9',2499],
}
shop_list=[]
shop_dic={}
def shop():
Tag=True
remain_Balance = Balance
print("开始购物".center(30,"="))
for key,value in product_dic.items():
print(key,value)
while Tag:
serial_number=input("输入你要购买商品的编号>>:").strip()
if serial_number.isdigit():
serial_number = int(serial_number)
if serial_number > 6:
print("请输入1-6")
continue
elif serial_number == "q":
print("开始结算".center(20,"="))
for list in shop_list:
product_name = list[0]
product_price = list[1]
shop_dic.setdefault(product_name, {})
shop_dic[product_name].setdefault("number", 0)
shop_dic[product_name].setdefault("total", 0)
shop_dic[product_name]["price"] = product_price
if product_name in shop_dic:
shop_dic[product_name]["number"] += 1
shop_dic[product_name]["total"] = shop_dic[product_name]["price"] * shop_dic[product_name]["number"]
# 总价
total = 0
for product in shop_dic:
print(product.center(7),str(shop_dic[product]["number"]).center(7),str(shop_dic[product]["total"]).center(5))
total = total + shop_dic[product]["total"]
print("您当前余额为{}".format(Balance).center(20))
print("您购买总价格为{}".format(total).center(20))
print("您的剩余价格为{}".format(Balance-total).center(20))
# with open('user.txt',"r",encoding="utf-8") as read_f,open('.user.txt.swap',"w",encoding="utf-8") as write_f:
# for line in read_f:
# line = line.replace(str(Balnce),str(Balance-total))
# write_f.write(line)
# os.remove('user.txt')
# os.rename('.user.txt.swap', 'user.txt')
with open('user.txt', "r", encoding="utf-8") as read_f, open('.user.txt.swap', "w",
encoding="utf-8") as write_f:
for line in read_f:
line = line.replace(str(Balance), str(Balance - total))
write_f.write(line)
os.remove('user.txt')
os.rename('.user.txt.swap', 'user.txt')
break
elif serial_number == "exit":
print("直接退出")
else:
continue
print("你要购买的商品编号{},商品{},价格{}".format(serial_number,product_dic.get(serial_number)[0],product_dic.get(serial_number)[1]))
yes_no=input("输入y/n,确定加入购物车>>:")
if yes_no == "y" :
if product_dic.get(serial_number)[1] > remain_Balance:
print("您的余额不够.无法加入到购物车,还差{}".format(product_dic.get(serial_number)[1]-remain_Balance))
else:
shop_list.append(product_dic.get(serial_number))
remain_Balance = remain_Balance - product_dic.get(serial_number)[1]
print(shop_list,remain_Balance)
continue
elif yes_no == "n":
print(shop_list)
pass
continue
elif yes_no == "exit":
break
else:
print("非法输入,请输入y或者n")
continue
user_info={}
while True:
print('''购物车小程序:
1、购物
2、注册账号
3、充值
输入q退出
''')
option=input("your option>>:").strip()
if not option.isdigit():
print("input 1 or 2\n")
continue
option=int(option)
if option > 2:
print("input 1 or 2\n")
continue
if option == 1:
Tag=True
count=0
while Tag:
input_user = input("your name>>:").strip()
with open("user.txt", encoding="utf-8", mode="r") as read_f:
for line in read_f:
line=line.strip("\n")
User=line.split(",")[0]
Password=line.split(",")[1]
Balance=line.split(",")[2]
if input_user == User:
user_info.setdefault(User,{})
user_info[User].setdefault("count",0)
user_info[User]["Password"]=Password
user_info[User]["Balance"]=Balance
if user_info[User]["count"] >= 3:
print("%s用户锁定" %(input_user))
Tag=False
break
else:
print("{}用户不存在".format(input_user))
continue
if Tag:
input_password = input("your password>>:").strip()
if input_password == user_info[input_user]["Password"]:
print("{}用户密码登陆正确".format(input_user))
print("用户:{},余额:{}".format(User, user_info[User]["Balance"]))
'''
这时候才开始购物
'''
Balance=int(Balance)
shop()
break
else:
user_info[input_user]["count"]+=1
print("{}用户密码登陆错误,还有{}尝试机会".format(input_user, 3 - user_info[input_user]["count"]))
elif option == 2:
Tag=True
while Tag:
with open('user.txt', "r", encoding="utf-8") as read_f, open('.user.txt.swap', "w",encoding="utf-8") as write_f:
register_user=input("你要注册的用户>>:").strip()
for line in read_f:
line=line.strip("\n")
User = line.split(",")[0]
Balance = line.split(",")[2]
if register_user == User:
print("{}用户已经存在".format(register_user))
yn_pay=input("是否充值y/n>>:")
if yn_pay == "y":
pay_money=input("你要充值多少>>:").strip()
print(Balance,pay_money)
print(line)
line=line.replace(Balance,str(int(Balance)+int(pay_money)))
write_f.write(line)
# os.remove("user.txt")
# os.rename('.user.txt.swap', 'user.txt')
elif yn_pay == "n":
pass
os.remove("user.txt")
os.rename('.user.txt.swap', 'user.txt')
python用字典编写购物程序_Python编写购物小程序相关推荐
- python制作微信小程序_python搭建微信小程序
微信官方发布小程序自动化测试框架Minium.该框架支持使用Python或者Javascript编写自动化测试程序,可以运行在IDE.Android以及iOS三端. 由于是微信官方发布,该框架注定秒杀 ...
- python聊天程序_python实现聊天小程序
本文实例为大家分享了python实现聊天小程序的具体代码,供大家参考,具体内容如下 我这里实现的是客户端与服务端进行通信的功能,比较简单,与上一篇文章的群聊不太一样. 服务端server.py #-* ...
- python编写学生选课系统程序_python面向对象编程小程序- 选课系统
选课系统 花了一晚上写的,可能还存在不足 1.程序框架 2.文件夹建立D:/选课系统 |___api | |___common_api.py |___bil | |___common.py |___c ...
- python编程超市购物系统_python面向过程编程小程序- 模拟超市收银系统
6.16自我总结 功能介绍程序功能介绍: 商品信息再读取修改买卖均已xlsx格式 且生成购物记录也按/用户名/购买时间.xlsx格式生成 账号密码输入错误三次按照时间进行冻结 用户信息已json格式保 ...
- python html5 便利店收银系统_python面向过程编程小程序- 模拟超市收银系统
6.16自我总结 功能介绍 程序功能介绍: 商品信息再读取修改买卖均已xlsx格式 且生成购物记录也按/用户名/购买时间.xlsx格式生成 账号密码输入错误三次按照时间进行冻结 用户信息已json格式 ...
- python收银小程序_python面向过程编程小程序- 模拟超市收银系统
6.16自我总结 功能介绍 程序功能介绍: 商品信息再读取修改买卖均已xlsx格式 且生成购物记录也按/用户名/购买时间.xlsx格式生成 账号密码输入错误三次按照时间进行冻结 用户信息已json格式 ...
- python代码测试健康指数计算器_Python入门级别的小程序(BMI计算器)
我是大涵呐,一个刚入Python的菜鸟,想通过自己了解的告诉同样刚学Python或者想学的朋友们一些基础的东西,希望对你们有所帮助. while True: # 1.输入身高(m).体重(kg) ,转 ...
- python背单词小程序_python背单词小程序
import random as t #创建单词序列 words=("easy","difficult","answer","co ...
- vue代码怎么变成小程序_使用vue编写h5公众号跳转小程序的实现代码
前言:我使用vue编写的h5公众号,实现点击小程序入口,打开小程序,微信官方文档:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_App ...
- c语言计算二次函数顶点坐标,C语言编写一个求一元二次方程的实根的程序。 编辑一个小程序去做一元二次方程的求解(b^24ac)...
导航:网站首页 > C语言编写一个求一元二次方程的实根的程序. 编辑一个小程序去做一元二次方程的求解(b^2>4ac) C语言编写一个求一元二次方程的实根的程序. 编辑一个小程序去做一元二 ...
最新文章
- 浅析 Mybatis 与 Hibernate 的区别与用途
- 在sql2005中加数据库时出现无法打开物理文件
- 2017年值得回首的那些事(文末有新春好礼相送)
- linux loop device
- mysql导入导出数据
- 50年代黄岩师专_300多位30、40和50年代获得第一份技术工作的开发人员的故事
- Windows 7密码重设盘的内部原理浅析
- exists sql用法_《SQL进阶教程》笔记(3)
- mysql中文坑爹的东西
- 如何做出优质的Scratch作品
- 密码学(一):古典密码之维吉尼亚密码原理介绍
- 这些配色方案让数据可视化图表更加高大上
- HDU--2015-TO-2019--假期实在是太无聊了....
- python爬取bilibili数据_BiliBili爬取数据简单分析
- 编译Android源码(9.0)
- 用HTML实现邮箱的注册页面
- mysql front的命令_mysql-front远程连接自己linux服务器上的mysql服务器
- 德赛西威IPU03域控制器方案介绍
- matlab求单自由度振动方程为,单自由度振动方程与Matlab/Simulink求解
- 认购与申购的区别是什么
热门文章
- C语言在不引入第三变量下交换俩数
- C++之多重继承引发的重复调用
- Spring boot的配置类
- JVM方法区内存分配
- MySQL查看存储过程
- python异常值检测的方法_Python实现非正太分布的异常值检测方式
- linux文件夹多个空格,linux-在“ for”循环中读取带有空格,带有多个输入文件的制表符的完整行...
- 10 分钟使用 Spring Boot + Vue + Antd + US3 搭建自己的图床
- 用这样的方法,我解决了leetcode的大部分的这种题型!
- 你所需要的java基础篇和提升篇大总结