购物车要求:

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

    文章目录 开发中,经常需要从一个已有的分支拉出一个新分支,去这个新分支做一些开发改动,这里示例为: 从master分支,重新拉取出一个新的分支,名字为dev,具体命令如下: 切换到被copy的分支(m ...

  2. 第一次刷Leetcode,为什么耗费很多时间

    Leetcode第2题思考过程分析:耗费的时间与思考过程 1. 审题耗费了很长时间,英文看不懂.两个单链表代表了两个整数,整数逆序,(2 -> 4 -> 3) + (5 -> 6 - ...

  3. linux下的命令是如何运行的

    linux下的命令分为内建命令.可执行文件.脚本文件 shell终端里键入一个命令,如ls.cd.bash,shell会先查询一个环境变量PATH,它存了各种可执行文件的路径,输入$PATH可以打印变 ...

  4. python Condition类(锁)

    Condition(条件变量)通常与一个锁关联.需要在多个Contidion中共享一个锁时,可以传递一个Lock/RLock实例给构造方法,否则它将自己生成一个RLock实例. 不理解锁的,请看上一条 ...

  5. QTP场景恢复函数

    public Function RecoveryFunction1(Object, Method, Arguments, retVal) Dim FileName ,TimeNow, ResPath ...

  6. css深入理解overflow

    1.基本属性 visible(默认值) 超出部分仍然正常显示 hidden 超出后隐藏 scroll 滚动条一致显示 auto 自适应 显示或隐藏滚动条 inherit overflow  =  ov ...

  7. C# 笔记 获取程序当前目录

    在C#中,我们有以下几种方式获取程序当前目录: Console.WriteLine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssem ...

  8. python面试题之请谈谈.pyc文件和.py文件的不同之处

    虽然这两种文件均保存字节代码,但.pyc文件是Python文件的编译版本,它有平台无关的字节代码,因此我们可以在任何支持.pyc格式文件的平台上执行它.Python会自动生成它以优化性能(加载时间,而 ...

  9. 【笔记目录2】【jessetalk 】ASP.NET Core快速入门_学习笔记汇总

    当前标签: ASP.NET Core快速入门 共2页: 上一页 1 2  任务27:Middleware管道介绍 GASA 2019-02-12 20:07 阅读:15 评论:0 任务26:dotne ...

  10. django-celery beat报错 error pid

    最近在用django-celery添加定时任务,测试时启动过一次Beat,beat按理说是只能启动一个的但是不服务器都重启过了还是提示已有进程在运行. ERROR: Pidfile (celerybe ...