作业2:购物车系统

#!/usr/bin/env python
# Author:Zhan Wei
'''购物车程序
作业需求:

目的:(1)启动程序后,让用户输入账号密码,正确后继续。
(2)用户输入工资,然后打印商品列表

(3)允许用户根据商品编号购买商品

(4)用户选择商品后,检测余额是否够,够直接扣款,不够就提醒

(5)可随时退出,退出时打印已购买商品和余额
'''

import os,sys,getpass
#购物车信息:
product_list=[("iwatch",1799),
("iphone",5669),
("ipad air",3299),
("Mi_8",2699),
("Huawei mate10",3599),
("Vivo NEX",4299)
]
shop_list=[]
print("欢迎来到购物乐园")
u=0
while u<3:
username = input("请输入您的用户名:") #使用input 让用户输入并赋值给username变量
user_file = open('account.txt','r') #打开account文件,权限是读取更新,并赋值给user_f变量
user_list = user_file.readlines() #使用.readlines的方法逐行读取account文件,并赋值给user_list变量
for user_line in user_list: #使用for循环读取account的内容
(user,passwd) = user_line.strip('\n').split() #分别获取账号和密码信息
if username == user: #使用if判断用户输入的用户是否在normal_user中存在
p = 0 #输入密码的循环次数
while p < 3: #只要用户登录异常不超过3次就不断循环
password = input('请输入您的密码:')
if password == passwd: #使用if判断用户输入的密码在normal_user中是否存在(相等)
print('欢迎 %s 登录系统' %username) #用户名密码全部相等(存在)打印欢迎登录信息
salary = input("input your salary:")
if salary.isdigit():
salary = int(salary)
while True:
print('#################购物清单###############')
for item in product_list:
print(product_list.index(item), item)
user_choice = input("which number of product do you want to buy?")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >= 0:
p_item = product_list[user_choice]
if p_item[1] <= salary:
shop_list.append(p_item)
salary = salary - p_item[1]
print("Added %s in shopping car,your current balance is %s" % (p_item, salary))
else:
print("your money is not enough,please try again")
else:
print("the code is not exist")
elif user_choice == 'no':
print("-----shoping list-----")
for p in shop_list:
print(p)
print("your current monney is", salary)
exit()
else:
print("wrong input")
else:
print("please input right salary^-^")
else:
if p != 2:
print('对不起,%s 的密码错误,请重新输入,您还有 %d 次机会'%(username,2 - p))
p += 1 #密码输入错误后,循环值增加1
else:
sys.exit('对不起 %s 用户不能继续尝试,请重新启动程序'% username)
else:
pass #当用户没匹配时,跳过并继续循环
posted @ 2018-08-02 10:47  湛位  阅读(178)  评论(0编辑  收藏  举报