|
#__author__ = 'leslie'
#-*- condig=utf-8 -*-
#购物车:
#1. 商品信息- 数量、单价、名称
#2. 用户信息- 帐号、密码、余额
#3. 用户可充值
#4. 购物历史信息
#5. 允许用户多次购买,每次可购买多件
#6. 余额不足时进行提醒
#7. 用户退出时 ,输出档次购物信息
#8. 用户下次登陆时可查看购物历史
#9. 商品列表分级显示
import os
import sys
name_list = {
'caosubo':['123456','9999'],'leslie':['2345','8888']
}
shop_list = ['家用电器','日用百货','服装鞋帽']
dianqi = ['电视','洗衣机','电冰箱']
yiyongbaihuo = ['洗衣粉','卫生纸','牙膏']
cloth = ['男装','女装','童装']
tv = [('创维',3000),('乐视',4000)]
washer = [('海尔',5000),('小天鹅',4500)]
icebox = [('海尔',6000),('格力',5500)]
detergent = [('雕牌',10),('立白',8)]
bumf = [('维达',15),('心相印',15)]
toothpaste = [('中华',12),('黑人',15)]
cloth_man = [('杉杉',200),('七匹狼',500)]
cloth_woman = [('普拉达',5000),('迪奥',10000)]
cloth_child = [('小天才',500),('巴拉巴拉',600)]
a = {'家用电器':dianqi,'日用百货':yiyongbaihuo,'服装鞋帽':cloth}
b = {'电视':tv,'洗衣机':washer,'电冰箱':icebox,'洗衣粉':detergent,'卫生纸':bumf,'牙膏':toothpaste,'男装':cloth_man,'女装':cloth_woman,'童装':cloth_child}
shop_car = {}
exit_flag = False
count = 0
while exit_flag is not True:
if count < 3:
username = input("请输入你的姓名:")
password = input("请输入密码:")
if username in name_list:
passwd = name_list[username][0]
salary = int(name_list[username][1])
if password == passwd:
f = open(username,'a')
print ("Welcome".center(40,'-'))
print ("您的账户余额为:%s"%salary)
money = input ("确定是否充值,输入金额,否请输入n:")
if money.isdigit():
money = int(money)
salary = salary + money
print ("你的余额为%s:"%salary)
break
elif money == 'n':
break
else:
print ("请重新输入!")
else:
print ("您的密码有误!请重新输入:")
count += 1
else:
print ("尚未注册")
count += 1
else:
exit()
while exit_flag is not True:
print ("商品分类".center(40,'-'))
for item in enumerate(shop_list):
print (item)
first_choice = input ("请输入你的选择:")
if first_choice.isdigit() :
first_choice = int(first_choice)
if first_choice < len((shop_list)):
shop1 = a[shop_list[first_choice]]
print ("商品分类".center(40,'-'))
for item in enumerate(shop1):
print (item)
second_choice = input ("请输入你的选择:")
if second_choice.isdigit():
second_choice = int(second_choice)
if second_choice < len(shop1):
shop2 = b[shop1[second_choice]]
print ("商品分类".center(40,'-'))
for item in enumerate(shop2):
print (item)
third_choice = input ("请输入你的选择:")
if third_choice.isdigit():
third_choice = int(third_choice)
if third_choice < len(shop2):
shop_choice = shop2[third_choice][0]
number = input ("购买数量:")
if number.isdigit():
number = int(number)
if number > 0:
shop_price = shop2[third_choice][1]*number
else:
print ("您的输入有误!")
else:
print ("您的输入有误!")
if salary > shop_price:
salary = salary - shop_price
shop_car[shop_choice] = number
print (shop_car)
else:
print ("你的余额不足!")
input_money = input ("需要充值吗?请输入金额或是q退出:")
if input_money.isdigit():
input_money = int (input_money)
salary = salary + input_money
print ("您现在的余额为:%s"%salary)
elif input_money == 'q':
pass
else:
print ("对不起你输入有误!")
else:
print ("您的选择有误,请重新选择!")
elif third_choice == 'q':
print ("欢迎下次光临!".center(40,'-'))
print ("您购买的商品如下:%s"%shop_car)
print ("您的余额为:%s"%salary)
exit_flag = True
else:
print ("您的输入有误!")
elif second_choice == 'q':
print ("欢迎下次光临!".center(40,'-'))
print ("您购买的商品如下:%s"%shop_car)
print ("您的余额为:%s"%salary)
exit_flag = True
else:
print ("您的输入有误!")
elif first_choice == 'q':
print ("欢迎下次光临!".center(40,'-'))
print ("您购买的商品如下:%s"%shop_car)
print ("您的余额为:%s"%salary)
exit_flag = True
else:
print ("您的输入有误!")
#将购物车信息存储在已用户名命名的文件中
f = open(username,'a')
bb = str(shop_car)
f.write(bb + '\n')
f.close() |
|