函數基礎實戰之ATM和購物車系統


username_list = []
prize_dict = {
    '0': "芭比娃娃",
    '1': "變形金剛",
    '2': "psp游戲機",
    '3': "奧特曼",
    '4': "遙控飛機",
    '5': "chongqiwawa",
}

shopping_car_dict = {}


def inp_username_pwd():
    username_inp = input('請輸入你的用戶名:')
    pwd_inp = input('請輸入你的密碼:')

    return username_inp, pwd_inp


def login():
    print('歡迎來到登錄功能')
    if username_list:
        print('已經登錄,請勿重復登錄')
        return

    count = 0
    while count < 3:
        username_inp, pwd_inp = inp_username_pwd()

        with open('user_info.txt', 'r', encoding='utf8') as fr:
            for user_info in fr:
                user_info = user_info.strip()
                username, pwd = user_info.split(':')

                if username == username_inp and pwd == pwd_inp:
                    print('登錄成功')
                    username_list.append(username_inp)
                    return

            else:
                print('賬號密碼錯誤')

            count += 1


def register():
    print('歡迎來到注冊功能')

    username_inp, pwd_inp = inp_username_pwd()

    with open('user_info.txt', 'a', encoding='utf8') as fa:
        fa.write(f'{username_inp}:{pwd_inp}\n')


def logout():
    print('歡迎來到注銷功能')

    if not username_list:
        print('請登錄后使用該功能')
        return

    username_list.clear()


def shopping():
    print('歡迎來到Nick集團消費功能')

    if not username_list:
        print('請登錄后使用該功能')
        return

    print('''
        0 芭比娃娃
        1 變形金剛
        2 psp游戲機
        3 奧特曼
        4 遙控飛機
        5 chongqiwawa
        ''')

    prize_choice = input('請輸入你需要購買的商品編號:')
    prize_name = prize_dict[prize_choice]

    if prize_name in shopping_car_dict:
        shopping_car_dict[prize_name] += 1
    else:
        shopping_car_dict[prize_name] = 1

    print(f'消費成功{prize_name},當前購物車情況為{shopping_car_dict}')


def shopping_car():
    print('恭喜剁手成功功能')

    if not username_list:
        print('請登錄后使用該功能')
        return

    print(f'恭喜你購物成功:{shopping_car_dict}')

    shopping_car_dict.clear()



func_dict = {
    '1': login,
    '2': register,
    '3': logout,
    '4': shopping,
    '5': shopping_car,
}

while True:
    print('''
    1 登錄
    2 注冊
    3 注銷
    4 購物
    5 購物車
    q 退出
    ''')
    func_choice = input('請選擇你要選擇的功能(輸入q退出):')
    if func_choice == 'q':
        break

    if func_choice not in func_dict:
        print('傻逼,英文看不懂正常,還看不懂阿拉伯數字')
        continue

    func_dict[func_choice]()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM