购物车要求:

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

    问题场景: 在阿里云搭建一个apache服务器,正常访问XAMPP目录下的页面. 服务器本地是可以正常访问的 但是远程 就不可以访问了: 出现这样的画面: 解决方法 1.按照提示找到httpd-xam ...

  2. 深度学习大规模MIMO中的功率分配

    摘要-本文使用深度学习的方法在大规模MIMO网络的下行链路中执行max-min和max-prod功率分配.更确切地说,与传统的面向优化的方法相比,训练深度神经网络来学习用户设备(UE)的位置和最优功率 ...

  3. 推荐系统之隐语义模型LFM

    LFM(latent factor model)隐语义模型,这也是在推荐系统中应用相当普遍的一种模型.那这种模型跟ItemCF或UserCF的不同在于: 对于UserCF,我们可以先计算和目标用户兴趣 ...

  4. sciencedirect 网站抓取过程

      开发环境 C#+SQLite 软件使用教程: 设置页面 1.        首先录入需要查询的关键词,如果需要根据年去查询,可以勾选对应的年,支持多个年份查询.点击[设置关键字]按钮,把待查询关键 ...

  5. Hibernate4教程二:基本配置

    可编程的配置方式一: 如果在配置cfg.xml的时候,不想在里面配置hbm.xml怎么办呢?可在程序里使用可编程的配置方式,也就是使用程序来指定在cfg.xml里面的配置信息,不推荐这种方式.如下: ...

  6. ftp搭建记录

    1.安装vsftpd的rpm包 rpm -ivh vsftpd-2.0.5-16.el5_4.1.i386.rpm 使用YUM命令安装 yum install vsftpd -y. 2.ftp命令 s ...

  7. 字符串String的使用方法

    var ddd = "举头望明月,低头思故乡" document.writeln(ddd.split(''));//选择字符串中的一个标识符,将字符串分割成数组; var slic ...

  8. 常看 Shell: 文本文件操作

    文件显示和信息 wc wc 可以用于统计文件的行数和单词数. nl nl 在文件的每行内容前面加上行号. 基于行的操作 grep grep 用于筛选匹配特定字符的行. grep "Hello ...

  9. PCA revisit

    都知道PCA可以做降维,那它为什么可以降维,究竟是怎么降维的呢? 1. 为什么我们要降维? 我们的样本数据好好的,为什么要去做降维,第一个要想清楚这个问题. 也许你是要训练一个分类器,觉得当前特征维度 ...

  10. 一次goldengate故障引发的操作系统hang起,HA自动切换

    现场: 跑着数据库的主机A报警应用连接不上数据库,我们无法ssh到主机.第一反应是通过telnet到远程控制口,发现数据库资源和硬件资源在被切换到HA架构的主机B(备机,通常性能比主机A的差,抗不住应 ...