import random
user_name = "guojie"
user_passwd = "123456"
shopping_list = {"苹果":5,"香蕉":10,"榴莲":15,"芒果":20,"梨":25}
#随机生成四位验证码
while True:
user_code = random.choice(range(1000, 10000)) # 生成四位验证码,刷新一次生成一次
user_code = ""#用于接收字符串
num1 = random.choice(range(0,4))
num2 = random.choice(range(0,4))
num3 = random.choice(range(0,4))
for i in range(0,4):#循环四次,生成四位验证码
if num1 == i:
temp = chr(random.randint(65,90))#生成A-Z
elif num2 == i:
temp = chr(random.randint(97,122))#生成a-z
elif num3 == i:
temp = random.choice(['!','@','#','$','%','^','&','*','?'])#随机生成一个特殊字符
else:
temp = random.choice(range(0,10))#随机生成一个数字
user_code += str(temp)
user_shopping = {} # 用户的购物
print("验证码:%s" %(user_code))
name = input("用户名:")
password = input("密码:")
code = input("验证码:")
if name == user_name and password == user_passwd:#先判断用户名和密码
if code == user_code:#验证验证码
str1 = "欢迎光临鲜果超市"
print("*"*5 + str1 + "*"*5)
#向客户展示商品
for i,j in shopping_list.items():
print(i,j,"元/个")
money = 0 # 金额
flag = False
while True:
#判断用户输入的是否存在清单中
user_input = input("购买:")
if user_input in shopping_list:
value = shopping_list[user_input]#返回键值
how_much = int(input("请问购买几斤?:"))
if user_input in user_shopping:#判断第二次购买的与第一次购买的物品相同
value2 = user_shopping[user_input]#返回已经在字典中值
user_shopping[user_input] = how_much+value2
money = money +(value * how_much)#单价*个数
# 将客户购买的水果和斤数放在客户的字典中
user_shopping.setdefault(user_input, how_much)
#判断用户是否继续购买
gogo = input("继续购买?:")
if gogo == "是":
flag = True
else:
print("欢迎下次光临,共支付%d元" %(money),"清单:",user_shopping)
break#跳出内层循环
else:
print("对不起,不存在该物品")
break#跳出外层循环
else:
print("验证码不对")
else:
print("用户名密码错误")