Python实现猜年龄游戏代码实例
作者:jueyuanfengsheng 时间:2021-01-17 09:53:04
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 = {
'1': register,
'2': 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)()
来源:https://www.cnblogs.com/zhangchaocoming/p/11552787.html
标签:Python,猜年龄,游戏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
CentOS 7 安装python3.7.1的方法及注意事项
2023-03-10 21:23:53
php函数之子字符串替换 str_replace
2023-11-23 17:32:29
PyQt5实现进度条与定时器及子线程同步关联
2023-03-20 14:48:35
![](https://img.aspxhome.com/file/2023/5/88875_0s.gif)
python3实现小球转动抽奖小游戏
2021-08-07 01:08:04
![](https://img.aspxhome.com/file/2023/1/125151_0s.jpg)
Python画图小案例之小雪人超详细源码注释
2021-09-21 11:49:44
python套接字socket通信
2023-01-26 17:51:03
![](https://img.aspxhome.com/file/2023/5/81875_0s.png)
MySQL数据库搜索中的大小写敏感性
2008-11-24 12:44:00
手把手教你安装Windows版本的Tensorflow
2021-07-12 07:03:15
![](https://img.aspxhome.com/file/2023/6/84066_0s.png)
PHP与Web页面交互操作实例分析
2023-09-05 14:43:16
![](https://img.aspxhome.com/file/2023/4/55604_0s.png)
关于自动化测试框架pytest的Fixture固件
2023-10-16 00:02:50
![](https://img.aspxhome.com/file/2023/9/97539_0s.png)
框架和框架之间的关系
2008-01-17 18:54:00
![](https://img.aspxhome.com/file/UploadPic/20081/17/2008117185848873s.jpg)
Python中的Matplotlib模块入门教程
2023-08-14 23:05:16
![](https://img.aspxhome.com/file/2023/8/99168_0s.png)
Golang嵌入资源文件实现步骤详解
2023-06-21 08:52:36
python数据结构之二叉树的统计与转换实例
2023-08-11 07:35:48
![](https://img.aspxhome.com/file/2023/2/61712_0s.jpg)
python统计日志ip访问数的方法
2023-07-05 18:57:20
Python获取当前时间的方法
2022-06-11 18:24:03
IE7异常CSS 导致内存破坏漏洞
2009-11-30 12:52:00
asp下为什么韩文字后面显示分号?
2011-03-10 11:07:00
Centos7下源码安装Python3 及shell 脚本自动安装Python3的教程
2023-01-11 13:45:04
Python基础之数据类型相关知识总结
2021-08-05 07:04:43
![](https://img.aspxhome.com/file/2023/6/110496_0s.png)