• 第十天作业


    用函数实现猜年龄游戏(含登陆注册功能)

    import random # 导入随机数模块
    
    def register():
        """注册"""
        print('注册功能')
        count = 0
        while count < 3:
            username_inp = input('请输入你的用户名:')
            pwd_inp = input('请输入你的密码:')
            re_pwd_inp = input('请再次输入你的密码:')
            if not pwd_inp == re_pwd_inp:  # 判断用户密码是否一致
                print('密码不一致')
                count += 1
                continue
            with open(f'user_info.txt', 'a', encoding='utf8') as fa:  # 将用户信息存入文件中
                fa.write(f'{username_inp}:{pwd_inp}
    ')
                fa.flush()  # 先保存再运行
                break
        login()
    
    
    
    def login():
        """登录"""
        print('登录功能')
        username_inp = input('请输入你的用户名:')
        pwd_inp = input('请输入你的密码:')
        with open(f'user_info.txt', 'r', encoding='utf8') as fr:
            for user_info in fr:
                username, pwd = user_info.split(':')
                if username.strip() == username_inp and pwd.strip() == pwd_inp:
                    print('登陆成功,是否开始游戏?')  # 询问用户是否开始游戏
                    while  True:
                        begin_game = input('请输入Y/N:')  # 判断用户的输入是否正确 
                        if begin_game != 'Y' and begin_game != 'N':
                            print('请正确输入')
                        if begin_game =='Y':  # 开始游戏就调用猜年龄函数
                            guess_age()
                            break
                        if begin_game=='N':
                            print('欢迎下次再来')
                            break
                else:
                    print('登录失败')
    
    def prize_info():
        """在文件中读取奖品"""
        with open(r'prize.txt', 'r', encoding='utf-8') as fr:
            prize_msg = fr.read()
        prize_dict = eval(prize_msg) # 使用eval函数将打开文件读取的字符串转为字典
        return prize_dict,prize_msg  
    # print(prize_dict)
    
    
    
    
    def guess_age():
        """猜年龄"""
        age = random.randint(1, 50) # 在1-50随机生成一个整数
        # print(age)
        age_count = 0
        while age_count < 3:
            age_inp = input('请输入你的猜测:')
            if not age_inp.isdigit():
                print('请输入一个数字:')
                continue
            age_inp_int = int(age_inp) # 转为整型
            if age_inp_int < age:
                print('太小了')
            elif age_inp_int > age:
                print('太大了')
            else:
                print('猜对了')
                get_prize(prize_info()[0], prize_info()[1]) # 猜对了调用获得奖品函数,实参以索引的形式调用返回值
                break
            age_count += 1
    
    
    
    def get_prize(prize_dict,prize_msg):  # 形参为在文件中读取奖品函数中的返回值
        """获得奖品"""
        get_prize_dic = {}
        print(f'奖品如下:
    {prize_msg}')
        prize_count = 0
        while prize_count < 2:
            prize_inp = input('请选择你想要的奖品:')
            if prize_inp not in prize_dict:   # 用户输入的奖品不对,请用户重新输入
                print('请重新选择')
                continue
            prize = prize_dict[prize_inp]
            print(f'恭喜获得奖励:{prize}')
            if prize not in get_prize_dic:
                get_prize_dic[prize] = 1
            else:
                get_prize_dic[prize] += 1
            prize_count += 1
        print(f'总共获得奖品:{get_prize_dic},游戏结束')
    
    
    
  • 相关阅读:
    Ubuntu上如何安装Java,Eclipse,Pydev,Python(自带,不用装),BeautifulSoup
    sed替换字符串时,使用正则表达式的注意事项
    shell实现trim函数-去除字符串两侧的空格(包括tab,space键)
    能用Shell就别编程-海量文本型数据的处理
    grep与egrep
    海量数据导入MySQL的注意事项
    Java读取文件
    Python爬虫经验
    Java字符串split函数的注意事项
    shiro+jwt+springboot理解
  • 原文地址:https://www.cnblogs.com/lyyblog0715/p/11551663.html
Copyright © 2020-2023  润新知