day_10猜年龄游戏函数版
'''
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猜年龄游戏函数版的更多相关文章
- python猜年龄游戏升级版
猜年龄游戏升级版 要求:允许用户最多尝试3次,每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y,就继续让其猜3次,以此往复,如果回答N,就退出程序,如何猜对了,就直接退出 age = 1 ...
- python基础实战之猜年龄游戏
目录 一.Python基础实战之猜年龄游戏 给定年龄,用户可以猜三次年龄 年龄猜对,让用户选择两次奖励 用户选择两次奖励后可以退出 age = 18 # 答案 count = 0 # 游戏次数控制 p ...
- day_06 猜年龄游戏,三级菜单 ,求1 - 2 + 3 - 4 + 5...99的所有数的和(课后作业)
1.猜年龄游戏: 要求: 允许用户最多尝试3次 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序 如果猜对了,有三次选择奖励 ...
- day07作业猜年龄游戏
# 给定年龄,用户可以猜三次年龄 # # 年龄猜对,让用户选择两次奖励 # # 用户选择两次奖励后退出 get_prize_dict = {} # 获取的奖品信息 age = 18 inp_count ...
- Python3 猜年龄小游戏进阶之函数处理
在猜年龄的基础上编写登录.注册方法,并且把猜年龄游戏分函数处理 登录函数 注册函数 猜年龄函数 选择奖品函数 # 注册 def register(): '''注册''' count = 0 while ...
- Python练习-猜年龄的LowB游戏
Alex大神今天让我做一个猜年龄的游戏: 第一个游戏是你只能猜三次:真的很LowB啊~ # 编辑者:闫龙 #猜年龄游戏,3次后程序自动退出! ages = 29; #for循环3次 for i in ...
- 用python写了一个猜年龄小游戏
写一个猜年龄游戏: 需要实现用户登录的功能 初始用户登录信息为 {'hades': '13579','nick': '123','ruixing': 'a1','fanping': 'b2'} 登录时 ...
- day03_11 if语句实现猜年龄01
老男孩猜年龄游戏 age_of_princal = 56 guess_age = int( input(">>:") ) #以下为伪代码 ''' if guess_ag ...
- Python字符串内置方法使用及年龄游戏深入探究
目录 作业 ==程序代码自上往下运行,建议自上而下的完成下列任务== 作业 使用代码实现以下业务逻辑: 写代码,有如下变量name = " aleX",请按照要求实现每个功能: 移 ...
随机推荐
- Python中常见的异常总结
Python中常见的异常总结 当Python检测到一个错误时,解释器就会指出当前流已经无法继续执行下去,这时候就出现了异常. 一.异常错误 a.语法错误 错误一: if ...
- C++统计程序运行时间代码片段
因为经常需要统计代码的运行时间,所以计时功能就显得很重要, 记录一下现在喜欢用的计时方式,供日后查阅. 1.下面是计时主函数, bool TimeStaticMine(int id,const cha ...
- shadow配置文件及结果
- python学习之函数(二)
4.4.6 动态传参 动态传参是针对形参而言 1.动态位置参数 在静态位置参数时,我们知道,定义函数时有几个位置参数,调用时就必须给几个实参,不能多也不能少.有时候,实际应用过程中,参数往往不能固 ...
- CentOS7中SSH免密登陆设置
一.准备三台客户机 hadoop100 192.168.13.100(Master) hadoop101 192.168.13.101 hadoop102 192.168.13.102 二.在hado ...
- POJ3585 Accumulation Degree【换根dp】
题目传送门 题意 给出一棵树,树上的边都有容量,在树上任意选一个点作为根,使得往外流(到叶节点,叶节点可以接受无限多的流量)的流量最大. 分析 首先,还是从1号点工具人开始$dfs$,可以求出$dp[ ...
- Java 读取application.properties配置文件中配置
实际开发中若需要读取配置文件application.properties中的配置,代码如下.例:读取配置文件中name属性配置值: 代码如下: import org.springframework.c ...
- cpu和内存的使用率统计
统计cpu和内存一个月的平均使用率: #!/bin/bash totalcpu_rate=0.0 totalmem_rate=0.0 num_days=$(ls -l /var/log/sa/sa[0 ...
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 F. Islands
On the mysterious continent of Tamriel, there is a great empire founded by human. To develope the tr ...
- phpstudy配置多站点
1.打开vhosts.conf文件 目录 Apache/conf/vhosts.conf #开启apache的vhost模块 (此模块默认是关闭的,去掉前面的#号) LoadModule vh ...