zoukankan      html  css  js  c++  java
  • 猜年龄升级版

    import os
    import random
    user_info = {
                'name': None,
                'password': None
            }
    login_dict = {'login': None}
    prize_dict ={
        '1': '女朋友',
        '2': 'ps4pro',
        '3': 's9世界赛决赛门票',
        '4': '霍格沃兹入学通知书',
        '5': '哆啦A梦',
        '6': '皮卡丘',
        '7': '核弹发射按钮',
        '8': '潘多拉魔盒',
        '9': '死亡笔记',
        '10': '幻想乡永久居住证'
    }
    
    
    def save(user_dict):
        DB_PATH = os.path.dirname(__file__)
        user_path = os.path.join(DB_PATH, user_dict['name'])
        with open(user_path, 'w', encoding='utf-8') as fw:
            user_dict = str(user_dict)
            fw.write(user_dict)
            fw.flush()
    
    def select(name):
        DB_PATH = os.path.dirname(__file__)
        user_path = os.path.join(DB_PATH, name)
        if os.path.exists(user_path):
            with open(user_path, 'r', encoding='utf-8') as fr:
                return eval(fr.read())
        return False
    
    def register():
        print('注册')
        a = 0
        while a < 3:
            name = input('请输入姓名:').strip()
            pwd = input('请输入密码:').strip()
            re_pwd = input('请确认密码:').strip()
    
            if pwd != re_pwd:
                print('两次密码输入不一致')
                a += 1
                continue
    
            res = select(name)
            if res:
                print('用户已注册!')
                break
    
            user_info['name'] = name
            user_info['password'] = pwd
            save(user_info)
            print('注册成功')
            break
    
    def login():
        print('登录')
        a = 0
        while a < 3:
            name = input('请输入姓名:').strip()
            pwd = input('请输入密码:').strip()
    
            user_dict = select(name)
            if not user_dict:
                print('请先注册')
                register()
    
            if pwd != user_dict['password']:
                print('密码输入错误')
                a += 1
                continue
    
            login_dict['login'] = name
            print('登录成功')
            break
    
    def guess():
        if not login_dict['login']:
            print('请先登录')
            login()
        age = random.randint(15, 20)
        chance = 5
        while chance > 0:
            guess = input('请输入猜测的年龄:').strip()
            if not guess.isdigit():
                print('请输入数字')
                continue
            guess = int(guess)
            if guess > age:
                chance -= 1
                print(f'猜大了,你还有{chance}次机会。')
                continue
            if guess < age:
                chance -= 1
                print(f'猜小了,你还有{chance}次机会。')
                continue
    
            print('恭喜你,猜对了!来选择两个奖品吧!')
            while True:
                print(prize_dict)
                choice1 = input('请选择第一件奖品编号').strip()
                if choice1 not in prize_dict:
                    print('请输入正确的编号')
                    continue
                prize1 = prize_dict[choice1]
                print(f'你的第一件奖品就是{prize1}啦')
                prize_dict.pop(choice1)
                break
            while True:
                print(prize_dict)
                choice2 = input('请选择第二件奖品编号').strip()
                if choice2 not in prize_dict:
                    print('请输入正确的编号')
                    continue
                prize2 = prize_dict[choice2]
                print(f'你的第二件奖品就是{prize2}啦')
                chance = 0
                break
    
    def run():
        register()
        login()
        guess()
    
    run()
    
  • 相关阅读:
    一句话解释各种虚拟币的用途
    php 网站301重定向设置代码实战案例
    seo网页加速技术,预加载 DNS Prefetching 详解
    AI赌神称霸德扑的秘密,刚刚被《科学》“曝光”了
    java实现 HTTP/HTTPS请求绕过证书检测代码实现
    pyspider源码解读--调度器scheduler.py
    pyspider操作千万级库,pyspider在对接量级较大库的策略
    尼克《人工智能简史》谈人工智能的历史、现实与未来
    CentOS7使用yum命令安装Java1.8
    php ci nginx 伪静态rewrite配置方法
  • 原文地址:https://www.cnblogs.com/binyuanxiang/p/11552348.html
Copyright © 2011-2022 走看看