python学习day3

1丶 用户先进行登录若是用户名在文件中且用户密码也正确就登录成功调用购物车函数,若是用户用户名输入正确密码错误,提示用户密码错误且从新输入,若是用户
输入用户名不存在,提示用户是否建立该用户,调用注册函数。app

1.一、用户登陆完成后,要求其输入初始金额,若是用户名或密码输入错误超过三次则退出程序。

二、 显示当前的主菜单列表 ["商品列表", "购物车", "充值"] 定义print_main_menu函数呈现主菜单

2.一、 商品列表: 定义一个商品字典 [{'name':'电脑','price':5000},{},{},...] 定义print_commodity_list函数呈现商品列表

2.二、 购物车: 用户购买的商品,造成一个列表呈现,以及金额的total, [{'name':'电脑','price':5000},{},{},] 定义print_shopping_car函数呈现购物车

2.三、 结算: 结算功能在购物车里,向用户提供结算功能,用户选择后 用户的初始金额 - 购物车中的总金额, 包括判断 "余额不足"

2.四、 充值: 当用户选择充值时,要求用户输入一个非负值,与用户的余额进行相加操做函数

# _*_ coding:utf-8 _*_"""解题思路建立用户登陆函数,用户登错3次程序退出建立新用户注册函数(若是用户用户名没在用户文件中提示是否新建用户)建立购物车函数"""import timedef login():    flag = False    count = 0    while True:        user_name = input("请输入用户名:")        password = input("请输入密码:")        # user_name = "dingyang"        # password = "12"        with open("login", encoding="utf-8", mode="r") as f:            for countent in f:                line = countent.strip().split()                #print(line)                if count == 2:                     print("您已经输错三次,已经不能再输入了。")                     exit()                if  user_name == line[0] and password == line[1]:                    flag = True                    break            if flag :                print("欢迎%s登录成功" % user_name)                shopping_car()                break            else:                with open("login", encoding="utf-8", mode="r") as f1:                    for counten in f1:                        line1 = counten.strip().split()                        # print(line1)                        if user_name != line1[0]:                            #print(line1[0])                            # while True:                            print("您输入的用户名不存在,须要注册%s吗?" % user_name)                            user_input = input("按y进行注册,按q从新输入:")                            if user_input == "y":                                print(user_name)                                user_register(user_name)                            elif user_input == "q":                                break                            else:                                print("您输入有误,请输入y或q")                        else:                            print("%s用户密码错误,请从新输入!" % user_name)                            break            count = count + 1def user_register(username):    while True:        register_passwd = input("请输入新用户密码:")        new_register_passwd =  input("请再次输入密码: ")        if register_passwd == new_register_passwd:            with open("login",encoding="utf-8",mode="a") as f2:                f2.write("\n%s " % username)                f2.write(new_register_passwd)                f2.close()                print("用户%s注册成功" % username)                login()                break        else:            print("您输入的密码不一致,请从新输入:")def shopping_car():    print("欢迎使用淘淘宝".center(50, "-"))    user_rmb()userRMB = 0def user_rmb():    while True:        try:            userRMB = int(input("请输入金额数(10-10000):"))        except ValueError as e:            print("输入非法,请从新输入")            continue        if userRMB < 10 or userRMB > 10000:            print("输入非法,请从新输入")        else:            breakmain_Menu = ["商品列表", "查看购物车", "充值"]def print_main_menu():    '''主菜单呈现'''    global main_Menu    for index,item in enumerate(main_Menu):        menu_index = index + 1        print("%d. %s" % (menu_index,item))commodity_List = [    {"name":"ThinkPad","price":3500},    {"name":"Iphone","price":4500},    {"name":"Three Squirrels","price":30},    {"name":"Ipod","price":500}]def print_commodity_list():    '''商品列表呈现'''    global commodity_List    print("商品列表".center(50, "-"))    for index, item in enumerate(commodity_List):        commodity_index = index + 1        print(str(commodity_index).center(10, " "), str(item["name"].ljust(31, " ")), str(item["price"]))def print_shopping_cart():    '''购物车列表呈现'''    global shopping_cart    print("购物车列表".center(50, "-"))    for index, item in enumerate(shopping_cart):        shopping_cart_index = index + 1        print(str(shopping_cart_index).center(10, " "), str(item["name"].ljust(31, " ")), str(item["price"]))shopping_cart = []#C =login()while True:    print_main_menu()    try:        menu_choice = int(input("请输入要选择的操做:"))    except ValueError as e:        print("输入非法,请从新输入")        continue    if menu_choice < 1 or menu_choice > len(main_Menu):        print("输入非法,请从新输入")    if main_Menu[menu_choice - 1] == "商品列表":        while True:            print_commodity_list()            try:                commodity_choice = int(input("请选择要购买的商品:"))            except ValueError as e:                print("输入非法,请从新输入")                continue            if commodity_choice < 1 or commodity_choice > len(commodity_List):                print("输入非法,请从新输入")                continue            shopping_cart.append(commodity_List[commodity_choice - 1])            print("添加购物车成功!")            user_operation = input("继续:y\n退出:e\n返回上一级:q ")            if user_operation == 'y':                continue            elif user_operation == 'e':                exit(0);            elif user_operation == 'q':                break            else:                print("输入非法,请从新输入")    elif main_Menu[menu_choice - 1] == "查看购物车":        while True:            total_consume = 0            print_shopping_cart()            for i in shopping_cart:                total_consume += i['price']            print("全部商品总金额为:", total_consume)            user_operation = input("当即结算:c\n退出:e\n返回上一级:q ")            if user_operation == 'c':                if userRMB > total_consume:                    userRMB -= total_consume                    print("结算成功:本次消费 %d,您的帐户余额为 %d" % (total_consume, userRMB))                    shopping_cart = []                else:                    print("您的帐户余额不足,请返回上一级充值后,再进行支付.")            elif user_operation == 'e':                exit(0);            elif user_operation == 'q':                break            else:                print("输入非法,请从新输入")    elif main_Menu[menu_choice - 1] == "充值":        while True:            try:                recharge_amount = int(input("请输入要充值的金额数,每次充值不得小于100"))            except ValueError as e:                print("输入非法,请从新输入")            if recharge_amount < 100:                print("输入金额过少,充值金额不得小于100")                continue            userRMB += recharge_amount            print("恭喜你充值成功,本次充值金额为 %d, 余额为 %d, 3秒后返回主菜单" % (recharge_amount, userRMB))            time.sleep(3)            break
相关文章
相关标签/搜索