• 函数处理


    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()
    
    
  • 相关阅读:
    Python3 session实现带密码访问网站后台
    Python3实现利用url请求百度翻译
    Python3 parse模块
    Python3 requests模块
    Nignx gzip 文件压缩
    Cnetos7 Yum安装Ningx
    喜极而泣,我终于学会了Nginx!
    Centos7 给磁盘创建Lvm虚拟盘
    Pyhton Tkinter图形工具(原创)
    nginx四种均衡策略
  • 原文地址:https://www.cnblogs.com/shaozheng/p/11550864.html
Copyright © 2020-2023  润新知