python 购物车+用户认证程序

时间:2021-08-08 15:19:22

创建文件a.txt,b.txt.c.txt用于存放应该持续保存的信息

a.txt :用户密码输入错误3次就锁定

b.txt :购物时的活动,每个用户只能参与一次

c:txt :购物完后的发票在这里查看

购物车程序:

  ready =
salary = input("how much is you salary:")
out =
#count =
if salary.isdigit():
salary = int(salary)
else:
exit("input string not digital....exit")
list = [["精选猪肉3斤", ], ["牛肉3斤", ], ["鸡肉三斤", ]]
shoppingcar = []
while True:
print("----------------------------------------------------------------------------")
print("Welcome to xiaodai store")
print("\033[31;1m目前正在做活动,购买商品满500的话就赠送一个精美的杯子哦!!!(超市内按h咨询)每人限1次 \033[0m")
for i in enumerate(list):
info_l = i[]
info_n = i[][]
info_m = i[][]
print(info_l, info_n, info_m)
user_c = input("please input your choose(h/help):")
if user_c.isdigit():
user_c = int(user_c)
if user_c < len(list) and user_c >= :
user_c = int(user_c)
money = list[user_c][]
shoppingcar_1 = list[user_c]
ready = ready + money
if salary >= money:
shoppingcar.append(shoppingcar_1)
print(shoppingcar_1, "add in then shoppingcar.....")
salary -= money
else:
print(" ----------------------")
print("|your money not enough:|", salary)
print(" ----------------------")
else:
print("没有这个商品编号")
elif user_c == "h" or user_c == help or user_c == "H":
print("q/quit quit store")
print("c/check check shopping car and salary")
print("0-2 choice of goods")
print("购买完商品后,输入[F]键来参加活动(满500赠送精美杯子)")
print("l/list 查看发票,购买完并退出超市后才能通过此方法看到发票")
elif user_c == "q" or user_c == "quit" or user_c == "Q":
print("your shoppingcar:", shoppingcar)
print("Be left over how much money:", salary)
time_1 = '%Y-%m-%d-%X'
time_2 = time.strftime(time_1)
with open('b.txt', 'a', encoding='utf-8') as f2:
f2.write("发票:%s用户 在 %s 购买了 %s 共消耗%s 元 \n" %(username,time_2,shoppingcar,ready))
exit("quit....")
elif user_c == 'l'or user_c =='list':
with open('b.txt','r+',encoding='utf-8')as f3:
print(f3.read())
elif user_c == "c" or user_c == "check" or user_c == "C":
print("Be left over how much money:", salary)
print(shoppingcar)
elif user_c == "f" or user_c == "F":
if ready >= :
with open('c.txt','r+',encoding='utf-8') as f4:
aready=f4.read()
if username not in aready:
print("你的满额任务达成,获得精美杯子一个..................")
shoppingcar.append(['精美杯子', ])
if len(aready) != :
f4.write('\n'+username)
else:
f4.write(username)
else:
print('你已经领取过奖励了,你的购物车:%s'%shoppingcar)
continue
else:
print("你没有满足活动的需求")
else:
exit("input error")

购物车

所有代码:

import time,sys
def shopping():
"'购物车程序,进入时输入有多少钱,然后进行商品购物,消费满500后选择接受礼品,礼品每个用户只能接受一次,接受过的用户存放在c.txt文件中,购物完毕后打印发票,发票存储在b.txt文件中,"
ready = 0
salary = input("how much is you salary:")
out = 0
#count = 0
if salary.isdigit(): #判断salary是否为数字
salary = int(salary)
else:
exit("input string not digital....exit")
list = [["精选猪肉3斤", 100], ["牛肉3斤", 200], ["鸡肉三斤", 50]]
shoppingcar = []
while True:
print("----------------------------------------------------------------------------")
print("Welcome to xiaodai store")
print("\033[31;1m目前正在做活动,购买商品满500的话就赠送一个精美的杯子哦!!!(超市内按h咨询)每人限1次 \033[0m")
for i in enumerate(list): #enumerate可以在读取数据时添加一个编号在第一位,如[0,"精选猪肉3斤", 100]
info_l = i[0]
info_n = i[1][0]
info_m = i[1][1]
print(info_l, info_n, info_m)
user_c = input("please input your choose(h/help):")
if user_c.isdigit():
user_c = int(user_c)
if user_c < len(list) and user_c >= 0:
user_c = int(user_c)
money = list[user_c][1]
shoppingcar_1 = list[user_c]
ready = ready + money
if salary >= money:
shoppingcar.append(shoppingcar_1)
print(shoppingcar_1, "add in then shoppingcar.....")
salary -= money
else:
print(" ----------------------")
print("|your money not enough:|", salary)
print(" ----------------------")
else:
print("没有这个商品编号")
elif user_c == "h" or user_c == help or user_c == "H":
print("q/quit quit store")
print("c/check check shopping car and salary")
print("0-2 choice of goods")
print("购买完商品后,输入[F]键来参加活动(满500赠送精美杯子)")
print("l/list 查看发票,购买完并退出超市后才能通过此方法看到发票")
elif user_c == "q" or user_c == "quit" or user_c == "Q":
print("your shoppingcar:", shoppingcar)
print("Be left over how much money:", salary)
time_1 = '%Y-%m-%d-%X'
time_2 = time.strftime(time_1)
with open('b.txt', 'a', encoding='utf-8') as f2:
f2.write("发票:%s用户 在 %s 购买了 %s 共消耗%s 元 \n" %(username,time_2,shoppingcar,ready))
exit("quit....")
elif user_c == 'l'or user_c =='list':
with open('b.txt','r+',encoding='utf-8')as f3:
print(f3.read())
elif user_c == "c" or user_c == "check" or user_c == "C":
print("Be left over how much money:", salary)
print(shoppingcar)
elif user_c == "f" or user_c == "F":
if ready >= 500 :
with open('c.txt','r+',encoding='utf-8') as f4:
aready=f4.read()
if username not in aready:
print("你的满额任务达成,获得精美杯子一个..................")
shoppingcar.append(['精美杯子', 0])
if len(aready) != 0 : #len可以打印这个变量中是否有数据,他会显示这个文件的字数,添加这个判断是格式问题,如果文件中没有数据,责直接将数据写入进去,不加空格,如果存在数据,责在插入数据前先执行回车.
f4.write('\n'+username) #在写入内容是在输入回车,不能用逗号来区分,用加号
else:
f4.write(username)
else:
print('你已经领取过奖励了,你的购物车:%s'%shoppingcar)
continue
else:
print("你没有满足活动的需求")
else:
exit("input error")
def auth():
'''慢慢的显示验证中'''
for i in range(1):
sys.stdout.write("验")
sys.stdout.flush()
time.sleep(0.3)
sys.stdout.write("证")
sys.stdout.flush()
time.sleep(0.3)
sys.stdout.write("中")
sys.stdout.flush()
time.sleep(0.3)
sys.stdout.write("请")
sys.stdout.flush()
time.sleep(0.3)
sys.stdout.write("稍")
sys.stdout.flush()
time.sleep(0.3)
sys.stdout.write("等")
sys.stdout.flush()
time.sleep(0.3)
user_list={
'xiaoming':{'xiaoming1'},
'xiaogang':{'xiaogang1'},
'xiaomei':{'xiaomei1'}
}
with open('a.txt','r+',encoding='utf-8')as f:
lock_list=f.read()
count=0
username = input(' please input your user name:')
for l in range(100):
passwd = input('\033[31;3m please input your passwd:\033[1m')
for i in user_list:
auth()
print('\n')
if username in lock_list :
print("用户已被锁定")
break
elif username not in user_list :
print('用户不存在')
username = input(' please input your user name:')
break
else:
a=user_list[username]
if passwd in a :
print('登录成功')
shopping()
elif count >= 3:
print("密码错误次数过多,用户%s被锁定,如需解锁,请联系管理员"%username)
f.write(username)
f.write('\n')
exit('quit...')
else:
print('密码错误登录失败,请重试')
count = count + 1
break #为了确保如"密码错误登录失败,请重试"提示只提示一次,而不是按照for循环的提示多次,当执行完后就退出for循环,退出后又继续加载这个循环。