购物车要求:

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

    基础类型 1.模板类cv::Vec<> 固定向量类,维度已知的小型向量——处理效率高 2.cv::Point类(Point2i,Point2f,Point2d;Point3i,Point3 ...

  2. css深入理解overflow

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

  3. LR 场景设置

    LR 场景设置group:多个脚本按照独立设置模式跑,各个脚本可以单独设置虚拟用户.运行时间scenario:多个脚本之间按照相同模式跑,将总的虚拟用户数按照一定比例分配给各个脚本 schedule ...

  4. Java接口自动化测试实战笔记

    综述 代码管理工具Git 测试框架 TestNG 测试报告 Mock 接口框架 HTTP 协议接口 测试框架 HttpClient SprintBoot 自动化测试开发 数据持久层框架 MyBatis ...

  5. CSS文本单行显示溢出时出现省略号,多行时首行缩进并出现省略号

    为了展示表格显示字数控制,比如商品类名字太长只展示部分 1.正常文本 效果: 2.单行时出现省略号 效果: 3.多行首行缩进并出现省略号 效果:

  6. WPFの触发器详解

    例子1 简单触发器Triggers——满足简答的条件,触发 <Window x:Class="Styles.SimpleTriggers" xmlns="http: ...

  7. idea maven打jar包

    双击clean install 会在根目录targer生成文件(注意删除test和替换yml文件)

  8. html打开子窗口

    第一个参数是打开的链接,第二个参数是窗口的名字,第三个参数是窗口的属性 window.open ("page.html", "newwindow", " ...

  9. 执行cython文件

    找到目录下的setup.py文件 cd到工程目录下: 执行 python3 setup.py build_ext --inplace

  10. mysql查询表的创建时间

    mysql查询表的创建时间 查询语句: SELECT table_name,create_time FROM information_schema.TABLES;