'''
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. 解决 /usr/lib/x86_64-linux-gnu/liblapack.so.3: undefined reference to `gotoblas'

    最近需要用到openblas库,本地ubuntu 系统内有一个版本,但是想用自己新编译的openblas, 使用cmake指定了链接的库地址, SET(LINK_LIB_DIRS "/usr ...

  2. 黑龙江网络安全技能竞赛awd后门分析复现

    0x0环境 0x1分析复现 0x2感想 围绕主办方留下的浅显后门可以打满整场,想拿第一还是要搞定深层后门

  3. python logger理解

    import logging#进行基本的日志配置 logging.basicConfig(filename = 'access.log',format = '%(asctime)s - %(name) ...

  4. linux whoami 显示当前用户的用户名

    [root@MongoDB ~]# whoami root

  5. C# StreamReader与StreamWriter

    原文:https://www.cnblogs.com/kissdodog/archive/2013/01/27/2878667.html StreamReader实现了抽象基类TextReader类, ...

  6. CDH的ntp时间同步

    云服务器: ntpq -p ntpdate -u 10.52.255.1  #手动同步 自建NTP服务器: https://www.cnblogs.com/yinzhengjie/p/9480665. ...

  7. Springg MVC 中文乱码处理

    1.对于post请求的处理方式,在web.xml添加拦截器 <filter> <filter-name>CharacterEncodingFilter</filter-n ...

  8. Java设计给小学生的自动出题系统

    系统要求: 1.自动出题,涉及加减乘除四则运算 2.运算为两位数之间 3.减法不能出现负数 4.乘法结果不超过100 5.除法必须整除 6.用户决定出题量 7.用户决定几道题一换行 8.题目不允许重复 ...

  9. POJ - 1287 Networking (最小生成树&并查集

    You are assigned to design network connections between certain points in a wide area. You are given ...

  10. 反向传播算法-损失函数&激活函数

    在监督学习中,传统的机器学习算法优化过程是采用一个合适的损失函数度量训练样本输出损失,对损失函数进行优化求最小化的极值,相应一系列线性系数矩阵W,偏置向量b即为我们的最终结果.在DNN中,损失函数优化 ...