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
相關文章
相關標籤/搜索