简单的购物车

def register():
username = input('请输入用户名(密码只能由数字字母组成):').strip()
if username.isalpha() or username.isdigit() or username.isalnum():
with open('register', encoding='utf-8')as f1:
for line in f1:
uname, pwd = line.strip().split('|')
if username == uname:
print('您的用户名已存在,请重新选择注册')
break
else:
while 1:
password = input('请输入密码(长度小于14):')
if len(password)<14:
with open('register', encoding='utf-8', mode='a')as f2:
f2.write('\n{}|{}'.format(username, password))
print('注册成功,请继续选择登录')
break
else:
print('您的密码过长,请重新输入')
else:
print('您的输入有误,请重新选择')
def lander():
sum = 3
flag=True
while flag:
if sum == 0:
print('对不起,您已经没有机会了,谢谢光临')
exit()
user_name = input('请输入您的用户名:').strip()
with open('register', encoding='utf-8')as f1:
for line in f1:
uname, pwd = line.strip().split('|')
if user_name == uname:
user1_password = input('请输入您的密码:').strip()
if user1_password == pwd:
print('密码正确,请继续选择购物')
flag=False
break
else:
print('密码输入有误')
break
else:
sum-=1
print('用户名输入错误或不存在,请重新输入,您还有%s次机会'%(sum))
def shopping():
while 1:
money = input('请输入充值金额:').strip()
if money.isdigit():
money = int(money)
print('您已成功充值%s元' % money)
break
else:
print('您输入的有非数字元素,请重新输入。')
flag = True
while flag:
print('有如下商品供您选择:')
for index, commodiety_dict in enumerate(goods):
print('{}\t{}\t{}'.format(index + 1, commodiety_dict['name'], commodiety_dict['price']))
print('n或者N\t购物车结算\nq或者Q退出程序(如不结算购物车可直接退出)')
select_sum = input('请输入你的选择:').strip()
if select_sum.isdigit():
select_sum = int(select_sum)
if 0 < select_sum <= len(goods):
if (select_sum - 1) not in goods_cars:
goods_cars[select_sum - 1] = \
{'name': goods[select_sum - 1]['name'], 'price': goods[select_sum - 1]['price'], 'amount': 1}
else:
goods_cars[select_sum - 1]['amount'] += 1
print('您选择的商品具体信息为:商品名称:{}\t商品价格:{}\t商品数量:1,已成功加入购物车。' \
.format(goods[select_sum - 1]['name'], goods[select_sum - 1]['price']))
else:
print('您输入的序号超出范围,请重新输入')
elif select_sum.upper() == 'N':
print('您购物车的具体商品如下:')
total_price = 0
for ind, com_dict in goods_cars.items():
print('序号:{}商品名称:{}商品单价:{}此商品总价:{}' \
.format(ind + 1, com_dict['name'], com_dict['price'], com_dict['price'] * com_dict['amount']))
total_price += com_dict['price'] * com_dict['amount']
print('------->总价格为:%s元' % total_price)
while 1:
if money >= total_price:
money -= total_price
print('您已经成功购买了以上商品,余额为%s元,谢谢光临~' % money)
flag = False
break
else:
print('余额不足,还差%s元' % (total_price - money))
choise_user=int(input('充值请按1,删除商品请按2\n请选择:'))
if choise_user==1:
money=int(input('请输入充值金额:').strip())
elif choise_user==2:
del_num = input('请输入要删除的商品序号:').strip()
if del_num.isdigit():
del_num = int(del_num)
if (del_num - 1) in goods_cars:
goods_cars[del_num - 1]['amount'] -= 1
if not goods_cars[del_num - 1]['amount']:
del goods_cars[del_num - 1]
else:
print('您输入的序号超出范围,请重新输入')
else:
print('您输入的有非数字元素,请重新输入')
elif select_sum.upper() == 'Q':
print('欢迎下次光临....')
break
else:
print('您输入的选项不存在,请重新输入')


goods = [{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
lst=['注册','登录','购物','退出']
goods_cars={}
while 1:
for i in lst:
print(lst.index(i) + 1, i)
user_choice=int(input('请输入您的选择:').strip())
if user_choice==1:
register()
elif user_choice==2:
lander()
elif user_choice==3:
shopping()
elif user_choice==4:
print('谢谢光临,祝您生活愉快,再见!')
break

猜你喜欢

转载自www.cnblogs.com/zhigu/p/9508047.html