day10 作业

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

  1. 登录函数

  2. 注册函数

  3. 猜年龄函数

  4. 选择奖品函数

# 先定义函数
def register():
    '''登录函数'''
    tag = True
    count = 0  # count用来控制登录失败次数,失败三次则停止继续登录
    while tag:
        name_inp = input('请输入用户名:').strip(' ')
        pwd_inp = input('请输入密码:').strip(' ')
        with open('user_info.txt','r',encoding='utf-8') as fr:
            user_info_list = fr.read().split('\n')
            for i in user_info_list:
                name,pwd = i.split(':')
                if name_inp != name:
                    print('未找到该用户!请注册!')
                    count += 1
                    break
                elif pwd_inp != pwd:
                    print('密码错误!请重新输入密码!')
                    count += 1
                    break
                else:
                    print('登陆成功')
                    tag = False
                    return 1


        if count == 3:
            tag = False
            print('登录过于频繁,请稍后再试!')


def login():
    '''注册函数'''
    tag = True
    while tag:
        log_name = input('请选择登录名:').strip(' ')
        log_pwd = input('请选择密码:').strip(' ')
        re_log_pwd = input('请确认密码:').strip(' ')

        if not log_pwd == re_log_pwd:
            print('两次密码输入不一致!请重新输入!')
            continue

        with open('user_info.txt','a+',encoding='utf8') as fa:
            fa.seek(0,0)  # 因为a+模式每次读取都会让光标移到最后,所以这里让光标前移
            user_info = fa.read()
            if user_info == '':  # 如果是第一次注册,文件为空,不这样下面会报错
                fa.write(f'{log_name}:{log_pwd}\n')
                fa.flush()
                print('注册成功')
                tag = False
                pass
            else:
                name_list = []
                user_info_list = user_info.split('\n')
                user_info_list.remove('')  # 最后一行会遗留一个空字符,把它删除
                for i in user_info_list:
                    name, pwd = i.split(':')
                    name_list.append(name)

                if  log_name in name_list:  # 判断此用户名是否已经注册
                    print('用户名已存在!')
                    pass
                else:  # 没注册过就注册
                    fa.write(f'{log_name}:{log_pwd}\n')
                    fa.flush()
                    print('注册成功')
                    tag = False





def guess():
    '''猜年龄游戏核心代码'''
    if reg == 1:  # 判断是否已经登录
        age = 18
        guess_inp = int(input('请输入你猜测的年龄:').strip(' '))
        if guess_inp < age:
            print('猜小了!')
        elif guess_inp > age:
            print('猜大了!')
        else:
            print('猜对了!!')
            return 1  # 返回值为1
    else:
        print('请先登录(1)!')


def choice_prize():
    '''选择奖品函数'''
    if res == 1:  # 判断是否已经完成猜年龄
        prize_list = [
            [0,'dolls'],
            [1,'杜蕾斯超薄'],
            [2,'如家7天优惠券'],
            [3,'汇仁肾宝'],
            [4,'tenga'],
            [5,'皮鞭'],
            [6,'蜡烛']
        ]

        prize_get = {}


        print('您可以选择两次奖品,奖品如下\n')
        for i in prize_list:
            print(i[0],i[1])  # 打印奖品列表

        time = 1  # 用来控制选奖品次数

        while time < 3:  # 只能选两次奖品

            choice_inp = input(f'请选择您第{time}次选择的奖品编号:').strip(' ')

            if not choice_inp.isdigit():
                print('输入错误!请输入数字编号!')

            choice = int(choice_inp)
            prize = prize_list[choice][1]

            if prize in prize_get:
                prize_get[prize] += 1
            else:
                prize_get[prize] = 1

            print(f'您选择了{prize}')

            time += 1

        print(f'恭喜您一共获得了{prize_get}')
    else:
        print('请先完成猜年龄(3)!')

# 下面开始正式运行
print('''
欢迎!请问您需要什么?
选择如下
1:登录
2:注册
3:猜年龄
4:选择奖品
q:退出程序
''')
reg = 0
res = 0

program = True
while program:
    want_inp = input('请做出你的选择(请输入编号)\n>>').strip(' ')
    if not want_inp.isdigit():
        program = False
        break
    want = int(want_inp)
    if want == 1:
        reg = register()
    elif want == 2:
        login()
    elif want == 3:
        res = guess()
    elif want == 4:
        choice_prize()

转载于:https://www.cnblogs.com/bowendown/p/11552223.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值