教你怎么写猜年龄游戏

  1. 在猜年龄的基础上编写登录、注册方法,并且把猜年龄游戏分函数处理,如
    2. 登录函数
    3. 注册函数
    4. 猜年龄函数
    5. 选择奖品函数
import time


def guess():
    count = 0
    age = 18
    while count < 3:
        age_inp = input('please enter the age you guessed:')
        if not age_inp.isdigit():
            print(f'wdnmd,are you {age_inp} years old?')
            print("restarting", end="")
            for i in range(5):
                print(".", end='')
                time.sleep(0.2)
            print(".")
            continue
        age_inp_int = int(age_inp)
        if age_inp_int < age:
            count += 1
            print(f"that's too young!you have {3 - count} more chance")
        elif age_inp_int > age:
            count += 1
            print(f"that's too old!you have {3 - count} more chance")
        else:
            print("that's right!")
            prize()
            break
    while count == 3:
        print("you lost")
        break


# guess()
def register():
    count = 0
    while count < 3:
        Username = input("Enter username:")
        password = input("Enter password:")
        re_password = input("Enter password again:")
        if not re_password == password:
            print('invaild enter')
            count += 1
            continue
        with open('userinfo.txt', 'a', encoding='utf8') as fa:
            fa.write(f'{Username}:{password}
')
            fa.flush()
            print('register succeed')
            break


def login():
    count = 0
    while count < 3:
        print("Now login!")
        Username_inp = input("Enter Your Username:")
        password_inp = input("Enter Your Password:")
        with open('userinfo.txt', 'r', encoding='utf-8')as fr:
            for userinfo in fr:
                username, password = userinfo.split(':')
                if username.strip() == Username_inp and password.strip() == password_inp:
                    print('登陆成功')
                    print("guessing", end="")
                    for i in range(5):
                        print(".", end='')
                        time.sleep(0.2)
                    print(".")
                    # prize()
                    guess()
                    count = 5
                    break
            else:
                print('登陆失败')
                count += 1


def prize():
    pr_msg = '''
    1.Thunderfury,Blessed Blade of the Windseeker
    2.Atiesh, Greatstaff of the Guardian
    3.Sulfuras, Hand of Ragnaros
    4.Warglaive of Azzinoth
    5.Thori'dal, the Star's Fury
    6.Shadowmourne
    7.Val'anyr, Hammer of Ancient Kings
    '''
    pr_dic = {
        '1': 'Thunderfury,Blessed Blade of the Windseeker',
        '2': 'Atiesh, Greatstaff of the Guardian',
        '3': 'Sulfuras, Hand of Ragnaros',
        '4': 'Warglaive of Azzinoth',
        '5': "Thori'dal, the Star's Fury",
        '6': 'Shadowmourne',
        '7': "Val'anyr, Hammer of Ancient Kings",
    }
    pr_csd_dic = {}
    pr_count = 0

    while pr_count < 2:
        print(f"there are the list of prize:{pr_msg}")
        pri = input(f"please enter the number of prize,to draw it,you can choose {2 - pr_count} times:")
        if pri not in pr_dic:

        # if pri not in lt:
            print('please enter 1 to 7')
            print("rechoosing", end="")
            for i in range(5):
                print(".", end='')
                time.sleep(0.2)
            print(".")
            continue
        pr = pr_dic[pri]
        if pr in pr_csd_dic:
            pr_csd_dic[pr] += 1
        else:
            pr_csd_dic[pr] = 1

        print(f"Now you got {pr}")
        pr_count += 1
        print(f"Finally you got {pr_csd_dic} congratulation")


func_msg={
    '1':register,
    '2':login,
}
while True:
    print('''
    1 register
    2 login
    q 退出
    ''')
    choice=input('choose something or press q to logout:').strip()
    if choice=='q':
        break
    if not choice.isdigit:
        print('please enter digit:')
        continue
    if choice not in func_msg:
        print('??? what is your problem:')
        continue
    func_msg[choice]()

原文地址:https://www.cnblogs.com/jimGraymane/p/11550602.html