'''
1. 在猜年龄的基础上编写登录、注册方法,并且把猜年龄游戏分函数处理,如
2. 登录函数
3. 注册函数
4. 猜年龄函数
5. 选择奖品函数
'''
import json
real_age = 18
prize_list = ['好迪洗发水', '绿箭侠', '小猪佩奇', '布娃娃', '再来一次!']
import random
user_prize_dict = {}
import os def register():
while True:
username = input('输入用户名>>>(q退出):').strip().lower()
if username=='q':break
password = input('请输入密码>>>:').strip()
re_password = input('请再次确认密码>>>:').strip()
if not password == re_password:
print('密码不一致,请重输!')
continue
user_dic = {'name': username, 'password': password}
json_user_dic = json.dumps(user_dic)
with open(f"{username}.txt", 'w', encoding='utf-8')as f:
f.write(json_user_dic)
f.flush()
print('注册成功!')
break def login():
count = 0
while True:
if count == 3:
print('错误输入次数过多!')
break
username = input('请输入用户名>>>:').strip()
if not os.path.exists(username + '.txt'):
print('该用户不存在!')
continue
password = input('请输入密码>>>:').strip()
with open(f"{username}.txt", 'r', encoding='utf-8') as f:
user_json_dic = f.read()
user_dic = json.loads(user_json_dic)
if username == user_dic['name'] and password == user_dic['password']:
print('登录成功!')
guess_age()
break
else:
print('用户名或密码错误!')
count += 1 def guess_age():
count = 0
print('现在进入猜年龄游戏环节.......\n')
while True:
count += 1
if count == 4:
print('抱歉!你三次都猜错了!')
again_guess_age = input('请问是否要继续猜3次(y继续,n退出)>>>:').strip().lower()
if again_guess_age == 'y':
count = 0
continue
break
age = input('请输入你的年龄>>>:').strip()
if not age.isdigit():
print('请输入纯数字!')
continue age = int(age)
if age > real_age:
print('猜大了!')
elif age < real_age:
print('猜小了!')
else:
print('恭喜你!猜对了!\n')
choice_prize()
break def choice_prize():
count = 1
print('进入抽奖环节.....,您共有两次机会!\n 奖品如下:')
while True:
for index, prize in enumerate(prize_list, 1):
print(index, prize)
choice = input('请按下按钮y随机选择奖品>>>:').strip().lower()
if not choice == 'y':
print('非法输入!')
continue
prize_choice = random.randint(1, 15)
if prize_choice in [6, 7, 8]:
prize_choice = 4
elif prize_choice in [9, 10, 11, 12, 13, 14, 15]:
prize_choice = 5
prize = prize_list[prize_choice - 1]
if prize in user_prize_dict:
user_prize_dict[prize] += 1
else:
user_prize_dict[prize] = 1
print(f'本次获得奖品为:{prize},您还有{2-count}次机会!\n')
if count == 2:
if user_prize_dict.get('再来一次!'):
user_prize_dict.pop('再来一次!')
print(f'总共获得的奖品为:{user_prize_dict}')
break
count += 1 user_func_dic = {
'': register,
'': login,
}
while True:
print('''
先注册,登陆后才能玩猜年龄游戏哦!
1. 注册
2. 登录
'''
)
choice = input('请选择功能编号(q退出)>>>:').strip().lower()
if choice == 'q' : break
if not choice in user_func_dic:
print('错误输入')
continue
user_func_dic.get(choice)()

day_10猜年龄游戏函数版的更多相关文章

  1. python猜年龄游戏升级版

    猜年龄游戏升级版 要求:允许用户最多尝试3次,每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y,就继续让其猜3次,以此往复,如果回答N,就退出程序,如何猜对了,就直接退出 age = 1 ...

  2. python基础实战之猜年龄游戏

    目录 一.Python基础实战之猜年龄游戏 给定年龄,用户可以猜三次年龄 年龄猜对,让用户选择两次奖励 用户选择两次奖励后可以退出 age = 18 # 答案 count = 0 # 游戏次数控制 p ...

  3. day_06 猜年龄游戏,三级菜单 ,求1 - 2 + 3 - 4 + 5...99的所有数的和(课后作业)

    1.猜年龄游戏: 要求: 允许用户最多尝试3次 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序 如果猜对了,有三次选择奖励 ...

  4. day07作业猜年龄游戏

    # 给定年龄,用户可以猜三次年龄 # # 年龄猜对,让用户选择两次奖励 # # 用户选择两次奖励后退出 get_prize_dict = {} # 获取的奖品信息 age = 18 inp_count ...

  5. Python3 猜年龄小游戏进阶之函数处理

    在猜年龄的基础上编写登录.注册方法,并且把猜年龄游戏分函数处理 登录函数 注册函数 猜年龄函数 选择奖品函数 # 注册 def register(): '''注册''' count = 0 while ...

  6. Python练习-猜年龄的LowB游戏

    Alex大神今天让我做一个猜年龄的游戏: 第一个游戏是你只能猜三次:真的很LowB啊~ # 编辑者:闫龙 #猜年龄游戏,3次后程序自动退出! ages = 29; #for循环3次 for i in ...

  7. 用python写了一个猜年龄小游戏

    写一个猜年龄游戏: 需要实现用户登录的功能 初始用户登录信息为 {'hades': '13579','nick': '123','ruixing': 'a1','fanping': 'b2'} 登录时 ...

  8. day03_11 if语句实现猜年龄01

    老男孩猜年龄游戏 age_of_princal = 56 guess_age = int( input(">>:") ) #以下为伪代码 ''' if guess_ag ...

  9. Python字符串内置方法使用及年龄游戏深入探究

    目录 作业 ==程序代码自上往下运行,建议自上而下的完成下列任务== 作业 使用代码实现以下业务逻辑: 写代码,有如下变量name = " aleX",请按照要求实现每个功能: 移 ...

随机推荐

  1. spring/boot 打包,资源/配置/业务文件分离

    spring/boot打包,将业务jar包和资源配置文件进行分离打包,打包后的资源在target/release文件夹下面 注意:添加以下配置后,注意修改自己的入口类 <!--相关编译打包依赖- ...

  2. 2 Configuring SAP ERP Sales and Distribution -introduction to SAP

    First Steps in SAPWe’ll now discuss some of the basic menus, screens, and transactions that you need ...

  3. HTTP Status 500 ? Internal Server Error

    getWriter()和getOutputStream()不能同时调用 HTTP Status 500 ? Internal Server Error Type Exception Report Me ...

  4. NFA转换为等价的DFA

    在编译系统中,词法分析阶段是整个编译系统的基础.对于单词的识别,有限自动机FA是一种十分有效的工具.有限自动机由其映射f是否为单值而分为确定的有限自动机DFA和非确定的有限自动机NFA.在非确定的有限 ...

  5. 龙芯软硬件培训个人总结-day1

    第一天主要针对的硬件设计,推他们年底要量产的3A4000+7A1000.这里我只记录下自己关注的几个点. 1,3A4000/3B4000处理器 支持256位向量指令:    对处理器封装进行了优化,不 ...

  6. hadop-eclipse-plugin导入plugins后没有mapreduce视图

    这种现象一般由于安装在eclipse\plugins下的插件没有导入的问题. 解决方法:把 eclipse\configuration\org.eclipse.update 删除掉.出现这种情况的原因 ...

  7. 为什么要malloc()?何时要malloc()?如何使用malloc()?

    今日写程序,突然想到一个问题,为什么有时候不要malloc,为什么有时候要呢!好好查资料才了解到一些原理. 函数原型:void *malloc(unsigned int num_bytes); //分 ...

  8. Nginx配置与使用

    一.简单介绍 由俄罗斯程序员IgorSysoev研发,2004年开源公布,特点是:内存cpu占用低,并发能力强,稳定,配置示例,反向代理:互联网企业 70%以上公司都在使用 nginx: 二.安装 1 ...

  9. Comet OJ - Contest #14

    Rank38. 还是比较不满意吧,C卡了太久,E没调出来,D也没空去做了. A 签到题. #include<bits/stdc++.h> using namespace std; #def ...

  10. 如何在一个线程环境中使用一个线程非安全的java类

    在开发过程中 当我们拿到一个线程非安全的java类的时候,我们可以额外创建这个类的管理类 并在管理类中控制同步 比如 一个非线程安全的Pair类 package test.thread.sx.test ...