函数处理

  1. 在猜年龄的基础上编写登录、注册方法,并且把猜年龄游戏分函数处理,如
    2. 登录函数

    1. 注册函数

    2. 猜年龄函数

    3. 选择奖品函数

def login():
    """登录"""
    count = 0
    while count <3:
        username_inp = input('请输入你的名字: ')
        pwd_inp = input('请输入你的密码: ')
        with open("useename_info.txt",'r',encoding='utf-8') as fr:
            for i in fr:
                i = i.split(':')
                username,pwd = i
                if  username_inp== username.strip() and pwd_inp == pwd.strip():
                    print("登陆成功!!")
                    count = 999
                    break
            else:
                    print("登陆失败!!")
                    count +=1
def register():
    """注册"""
    count = 0
    while count <3:
        username = input('请输入你的名字: ')
        pwd = input('请输入你的密码: ')
        pad = input('请再次输入你的密码: ')

        if not pwd ==pad:
            print('不好意思!你的密码错误!!!')
            count +=1
            continue

        with open("useename_info.txt",'a',encoding='utf-8') as fa:
            fa.write(f'{username} : {pwd}
')
            fa.flush()
            break
def game():
    """核心"""
    count = 0
    age_inp = 28
    while count < 3:
        age = input('请输入你的年龄:')

        if not age.isdigit():
            print(f'你的年龄是{age}吗??')
            continue
        age_int = int(age)

        if age_int > age_inp:
            print('猜大啦!')
        elif age_int < age_inp:
            print('猜小啦!!')
        else:
            print('猜对啦!!')
            prize()
            break
    count +=1

def prize():
    """奖品"""
    msg = '''
    '0': '娃娃‘,
    '1': '变形金刚'
    '2': 'psp游戏机'
    '3': ' 玩具车'
    '4': '童话书'
    '5': '<墨菲定律>'
    '''
    prize_dict = {
        '0': '娃娃',
        '1': '变形金刚',
        '2': 'psp游戏机',
        '3': ' 玩具车',
        '4': '童话书',
        '5': '<墨菲定律>',
    }
    get_prize_dict = {}
    choice_count = 0
    while choice_count <2:
        print(f'奖品如下:{msg}')
        choice = input(f'请输入你想要的奖品: ')
        prize_choice = prize_dict[choice]
        if prize_choice in prize_dict:
            get_prize_dict[prize_choice] +=1
        else:
            get_prize_dict[prize_choice] = 1
        print(f'恭喜你获得奖品为: {prize_choice}')
        choice_count +=1
    print(f'总共获得奖品为:{get_prize_dict}')

def main():
    register()
    login()
    game()


if __name__=="__main__":
    main()

原文地址:https://www.cnblogs.com/shaozheng/p/11550864.html