Python购物车小程序

#!/usr/bin/env python
#-*- coding:UTF-8 -*-
#----------------------------------------------------------------------------------------------------------------------
#说明:
# 购物车程序:
# 1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表
# 2、允许用户根据商品编号购买商品
# 3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
# 4、可随时退出,退出时,打印已购买商品和余额
# 5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
# 6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
# 7、允许查询之前的消费记录
#-----------------------------------------------------------------------------------------------------------------------
import os
#定义代码运行时处理数据的字典
dict_user_info= {}
#定义代码运行时处理数据的列表
shoped = []
#退出标志
flag = 0

def write_file(salary,shoped,name):
f1 = open("%s-new.txt" % name, 'a', encoding="utf-8")
shoped = ",".join(shoped)
f1.write("salary:%s\n" % salary)
f1.write("shoped:%s" % shoped)
f1.close()
os.remove("%s.txt"% name)
os.rename("%s-new.txt" % name,"%s.txt" % name)
return 0

######################格式化输出#########################
info = '''
------------------List------------------
1.足球: {a}
2.篮球: {b}
3.网球: {c}
4.橄榄球: {d}
5.退出
6.查询消费记录'''.format(a=100,b=120,c=80,d=50)
#########################################################

while flag ==0:
name = input("用户名:")
passwd = input("密码:")

if not os.path.exists("%s.txt" % name):
salary = int(input("请输入工资:"))
print(info)
dict_user_info["salary"] = salary
dict_user_info["shoped"] = shoped
f = open("%s.txt" % name, 'a',encoding = "utf-8")
f.write("salary:%s\n" % salary)
f.write("shoped:")
f.close()

while True:
print(info)
choice = input("请选择:")
if choice == "1" or choice == "足球":
if salary - 100 >= 0:
salary -= 100
print(salary)
shoped.append("足球")
else:
print("余额不足")
write_file(salary=salary,shoped=shoped,name=name)
flag = 1
break
if choice == "2" or choice == "篮球":
if salary - 120 >= 0:
salary -= 120
print(salary)
shoped.append("篮球")
else:
print("余额不足")
write_file(salary=salary, shoped=shoped, name=name)
flag = 1
break
if choice == "3" or choice == "网球":
if salary - 80 >= 0:
salary -= 80
print(salary)
shoped.append("网球")
else:
print("余额不足")
write_file(salary=salary, shoped=shoped, name=name)
flag = 1
break
if choice == "4" or choice == "橄榄球":
if salary - 50 >= 0:
salary -= 50
print(salary)
shoped.append("橄榄球")
else:
print("余额不足")
write_file(salary=salary, shoped=shoped, name=name)
flag = 1
break
if choice == "5" or choice == "退出":
write_file(salary=salary, shoped=shoped, name=name)
print("欢迎下次再来!")
flag = 1
break
if choice == "6" or choice == "查询消费记录":
print("余额为%s" % salary)
print(dict_user_info["shoped"])
else:
with open("%s.txt" % name,'r+',encoding = 'utf-8') as f:
for i in f:
if "salary" in i:
i = i.strip().split(":")
salary = int(i[1])
print("余额为%s" % salary)
if "shoped" in i:
i = i.strip().split(":")
shoped = i[1].split(",")
print("已购买%s" % shoped)
dict_user_info["salary"] = salary
dict_user_info["shoped"] = shoped
choice1 = input("继续购买请按1,退出请按2")
if choice1 == "2":
flag = 1
break
if choice1 == "1":

while True:
print(info)
choice1 = input("请选择:")
if choice == "1" or choice == "足球":
if salary - 100 >= 0:
salary -= 100
print(salary)
shoped.append("足球")
else:
print("余额不足")
write_file(salary=salary, shoped=shoped, name=name)
flag = 1
break
if choice1 == "2" or choice == "篮球":
if salary - 120 >= 0:
salary -= 120
print(salary)
shoped.append("篮球")
else:
print("余额不足")
write_file(salary=salary, shoped=shoped, name=name)
flag = 1
break
if choice1 == "3" or choice == "网球":
if salary - 80 >= 0:
salary -= 80
print(salary)
shoped.append("网球")
else:
print("余额不足")
write_file(salary=salary, shoped=shoped, name=name)
flag = 1
break
if choice1 == "4" or choice == "橄榄球":
if salary - 50 >= 0:
salary -= 50
print(salary)
shoped.append("橄榄球")
else:
print("余额不足")
write_file(salary=salary, shoped=shoped, name=name)
flag = 1
break
if choice1 == "5" or choice == "退出":
write_file(salary=salary, shoped=shoped, name=name)
print("欢迎下次再来!")
flag = 1
break
if choice1 == "6" or choice == "查询消费记录":
print("余额为%s" % salary)
print(dict_user_info["shoped"])


posted on 2017-09-06 00:13  天天吃炒饭  阅读(77)  评论(0)    收藏  举报

导航