购物车要求:

       用户名和密码存放于文件中
启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额
#!/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编写购物小程序的更多相关文章

  1. python编写购物车小程序

     #练习#程序购物车#启动程序后,让用户输入工资,  然后打印商品列表,允许用户根据商品编号购买商品用户选择商品后 #检测余额是否够,够就直接扣款,不够就提醒可随时退出,退出时,打印已购买商品和余额  ...

  2. python 的 购物小程序

    money = input('请输入您的工资:') shop = [("iphone",5800),("ipod",3000),("book" ...

  3. 第一次用python编写的小程序

    print ("*******数字游戏*********")temp = input ("猜猜小红现在心里想的是什么数字呢?")guess = int(temp ...

  4. Python实现购物小程序

    一.需求 1.登录 { ‘xxx1’:{'passwd':'123','role':1,'moeny':10000,"carts":['mac']}, 'xxx1':{'passw ...

  5. python学习day4--python基础--购物小程序

    '''购物小程序:用户启动时先输入工资用户启动程序后打印商品列表允许用户选择购买商品允许用户不断购买各种商品购买时检测余额是否够,如果够直接扣款,否则打印余额不足允许用户主动退出程序,退出时打印已购商 ...

  6. Python编写简易木马程序(转载乌云)

    Python编写简易木马程序 light · 2015/01/26 10:07 0x00 准备 文章内容仅供学习研究.切勿用于非法用途! 这次我们使用Python编写一个具有键盘记录.截屏以及通信功能 ...

  7. 基于php基础语言编写的小程序之计算器

    基于php基础语言编写的小程序之计算器 需求:在输入框中输入数字进行加.减.乘.除运算(html+php) 思路: 1首先要创建输入数字和运算符的输入框,数字用input的text属性,运算符用sel ...

  8. Python编写守护进程程序

    Python编写守护进程程序思路 1. fork子进程,父进程退出通常,我们执行服务端程序的时候都会通过终端连接到服务器,成功连接后会加载shell环境,终端和shell都是进程,shell进程是终端 ...

  9. 【Python精华】100个Python练手小程序

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同 ...

随机推荐

  1. taintCheck的实现

    参考:http://bitblaze.cs.berkeley.edu/papers/taintcheck-full.pdf 1. 应用taint analysis需要解决三个问题 a. 哪些input ...

  2. 视区相关单位vw, vh ,vm,CSS/CSS3长度、时间、频率、角度单位大全

    一.CSS长度值 em 相对于父元素的字体大小 ex 相对于小写字母"x"的高度 gd 一般用在东亚字体排版上,这个与英文并无关系 rem 相对于根元素字体大小 vw 相对于视窗的 ...

  3. 路由参数 query和params

    1. path:'www.baidu.com' query  { id:122 } 对应地址:http:'www.baidu.coom?id=122'   类似get方式 2.name:'baidu' ...

  4. javascript中数组的应用总结

    最近在总结javascript的相关应用,今天对js中的数组部分进行归纳总结,以便在以后的工作中有所参考. 1.在js中数组的定义方式有两种: var a = [1,2,3,4]; var b = n ...

  5. python序列基本操作

    这里讲一基本概念:容器---可以包含其他对象的对象:两种主要的容器是序列(列表和元祖)和映射(字典) 关于序列的通用基本操作:python中常用的序列主要有两种:列表和元祖  -------索引,切片 ...

  6. 【问题解决方案】git/github本地和远程仓库的重命名

    参考: CSDN博文:在Github上重命名仓库 背景: 偶然终于看到一条规范里写着: "通常(注意是通常,尤其是 Web 相关的项目) repo 的命名用小写英文,多个字母之间用连字符(比 ...

  7. 利用Kubernetes(K8S)部署JAVA项目

    一.jar包和war包区别 首先简单介绍一下jar包和war包区别,当时就没分清,导致部署总是傻傻分不清楚. jar包:jar包就是java的类进行编译生成的class文件就行打包的压缩包.里面是一些 ...

  8. Redis的备份与恢复

    备份 dump.rdb:RDB方式的备份文件 appendonly.aof:AOF方式的备份文件 rdb 备份处理 # 编辑redis.conf文件,找到如下参数,默认开启. save 900 1 s ...

  9. postgresql中rank() over, dense_rank(), row_number() 的用法和区别

  10. Anaconda/Conda创建环境时报错的解决方案

    按照Conda网站上的提示安装完Conda之后,想要用conda create创建环境,一直报错: ERROR conda.core.link:_execute_actions(337): An er ...