Python实现猜年龄游戏代码实例
作者:jueyuanfengsheng 发布时间:2021-01-17 09:53:04
标签:Python,猜年龄,游戏
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


猜你喜欢
- 全文检索引擎入门灰常不幸的是,关系型数据库对全文检索的支持没有被标准化。不同的数据库通过它们自己的方式来实现全文检索,而且SQL
- 嗯,你可以说我很无聊。最近疯狂加班,今天才得以有时间搞一个CSS的像素图来消遣休息下。先看效果:运行代码框<!DOCTYPE html
- 想学习Python的人都会有一个困惑,那就是Python目前有两个版本Python2和Python3,Python2与Python3有何区别
- 研究好多天了,也试过好多办法了,总结出目前发现最好的方法:先说一下基本的东西:<%@ codepage=65001%>
- 简介可以考虑以下几个可能的应用:如果你需要对文件进行校验或验证,你可以使用MD5码来检查文件是否被篡改或损坏。如果你需要对文件进行分类或去重
- 正在看的ORACLE教程是:MySQL安全性指南(3)(转)。 &nb
- 废话不多说1.win+R 启动“运行”输入cmd 点确定2.输入 cd /d xxxxxxx回车jupyter notebook回车在这里我
- 1、问题在使用Python中pandas读取csv文件时,由于文件编码格式出现以下问题:Traceback (most recent cal
- python封装简介1.效果图:对比一:对比二:2.学习来源代码:# 封装是面向对象的三大特性之一# 封装指的是隐藏对象中一些不希望被外部所
- processlist命令的输出结果显示了有哪些线程在运行,可以帮助识别出有问题的查询语句,两种方式使用这个命令。1.  
- vue动态添加store,路由和国际化vue动态添加store想写组件库?用这个吧 …// store module标
- # coding:utf-8 import os import sys def cut_and_paste_file(source, des
- 本篇文章将实现用Python获取系统信息CPU占有率并将其变化绘制成折线图。第一步:获取系统CPU占有率信息psutil模块是专门获取系统信
- 本文实例讲述了Python socket实现的简单通信功能。分享给大家供大家参考,具体如下:套接字(socket)是计算机网络数据结构,在任
- 本文实例为大家分享了PHP变量传值赋值和引用赋值变量销毁的具体代码,供大家参考,具体内容如下<?php $a = 100
- toString()方法的定义和用法:toString()方法可以把Number对象转换成字符串,并返回此字符串。点击可查看更多
- 链接的 target 属性怎么用 JS 来控制? 在HTML 4.0 Strict和XHTML 1.0 STRICT里不允许在<a&g
- 0x00 识别涉及技术验证码识别涉及很多方面的内容。入手难度大,但是入手后,可拓展性又非常广泛,可玩性极强,成就感也很足。验证码图像处理验证
- 任何一个交互过程的操作,对于用户来说都有学习成本,谁也不能保证所有人都可以准确无误地走完一个流程。交互设计师在设计时应该考虑适时地给用户相应
- 目录前言什么是 VueUse简单上手还有我们熟悉的 防抖 和 节流还还有全局状态共享的函数更多前言上次在看前端早早聊大会中, 尤大大再一次提