python实战之90行代码写个猜数字游戏


Posted in Python onApril 22, 2021

一、导入库

import random
import time

二、注册用户

我们用变量与input实现

name = str(input('请输入用户名:'))
print('欢迎您,'+name)

三、注册年龄

这里我们得用except制作乱输文本就游戏结束的程序

乱输文本就结束

try:
    age = int(input('请输入年龄:'))
except ValueError:
    print('非法输入')
    age = 30000

顺便把年龄设为30000[滑稽]

再根据年龄大小分配金币

四、分配金币

if age <10:
    gold = 500
    print('您的初始金币为500')
if age <20 and age >10:
    gold = 1000
    print('您的初始金币为1000')
if age >20 and age <30:
    gold = 1500
    print('您的初始金币为1500')
if age >30 and age <1000:
    gold = 200
    print('年龄过大,您的初始金币为200')

用if语句保证金币小于10000,如果等于10000,游戏结束

五、if判断 1

if gold !=10000:
    Game_start = str(input('是否开始游戏?(True或False)'))
    while gold !=10000:

接下来就是最重要的游戏部分了,认真!

我们还得用if语句判断Game_start变量为True,并创建一个随机数列表

六、if与随机数

if Game_start =='True':
	list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
	while list_123 ==10:
		list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]

然后设定回答变量和正确答案与错误答案

七、回答、正确与错误

answer = str(input('请猜数(big或small):'))
if list_123[0] + list_123[1] + list_123[2] >10:
	result = 'big'
	error_result = 'small'
if list_123[0] + list_123[1] + list_123[2] <10:
	result = 'small'
	error_result = 'big'

还得用if语句判断正确、错误与非法输入的答案

八、if判断 2

if answer ==result:
	print('您赢了!')
	gold = gold + 100
	print('随机数为'+str(list_123))
	print('您现在有'+str(gold)+'金币')
elif answer ==error_result:
	print('您输了...')
	print('随机数为'+str(list_123))
	gold = gold - 100
	print('您现在有'+str(gold)+'金币')
else:
	print('非法输入')
	gold = 10000

再制作购买道具的模块

太长了我懒得写一句一句解析

九、购买道具

if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000:
    answer = str(input('您现在可以购买道具,是否购买?(True或False):'))
    if answer =='True':
        print('请说出要购买der道具')
        print('*'*41)
        print('*金币翻倍器[现有的]   金币翻倍器[得到的]*')
        print('*     2000G                  1500G      *')
        print('*'*41)
        answer = str(input('请说出要购买der道具(金币翻倍器[现有的]说1,金币翻倍器[得到的]说2):'))
        if gold >2000 and answer =='1':
            gold = gold - 2000
            answer = int(input('请说出要翻der倍数:'))
            gold = str(gold * answer)
            print('您现在有'+gold+'金币')
            gold = int(gold)
        elif gold >1500 and answer =='2':
            gold = gold - 1500
            answer = int(input('请说出要翻der倍数:'))
            gold_mang = 100 * answer
            gold = gold + gold_mang
            print('您现在有'+str(gold)+'金币')
            gold = int(gold)
            else:
                print('没有这个道具,游戏结束')
                gold = 10000
            elif answer =='False':
                print('继续游戏')
            else:
                print('非法输入')
                gold = 10000

我写到一半发现没有把上面的桥接上,所以现在来桥接

十、桥接

import random
import time
name = str(input('请输入用户名:'))
print('欢迎您,'+name)
try:
    age = int(input('请输入年龄:'))
except ValueError:
    print('非法输入')
    age = 30000
    gold = 10000
if age <10:
    gold = 500
    print('您的初始金币为500')
if age <20 and age >10:
    gold = 1000
    print('您的初始金币为1000')
if age >20 and age <30:
    gold = 1500
    print('您的初始金币为1500')
if age >30 and age <1000:
    gold = 200
    print('年龄过大,您的初始金币为200')
if gold !=10000:
    Game_start = str(input('是否开始游戏?(True或False)'))
    while gold !=10000:
        if Game_start =='True':
            list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
            while list_123 ==10:
                list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
            answer = str(input('请猜数(big或small):'))
            if list_123[0] + list_123[1] + list_123[2] >10:
                result = 'big'
                error_result = 'small'
            if list_123[0] + list_123[1] + list_123[2] <10:
                result = 'small'
                error_result = 'big'
            if answer ==result:
                print('您赢了!')
                gold = gold + 100
                print('随机数为'+str(list_123))
                print('您现在有'+str(gold)+'金币')
            elif answer ==error_result:
                print('您输了...')
                print('随机数为'+str(list_123))
                gold = gold - 100
                print('您现在有'+str(gold)+'金币')
            else:
                print('非法输入')
                gold = 10000
            if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000:
                answer = str(input('您现在可以购买道具,是否购买?(True或False):'))
                if answer =='True':
                    print('请说出要购买der道具')
                    print('*'*41)
                    print('*金币翻倍器[现有的]   金币翻倍器[得到的]*')
                    print('*     2000G                  1500G      *')
                    print('*'*41)
                    answer = str(input('请说出要购买der道具(金币翻倍器[现有的]说1,金币翻倍器[得到的]说2):'))
                    if gold >2000 and answer =='1':
                        gold = gold - 2000
                        answer = int(input('请说出要翻der倍数:'))
                        gold = str(gold * answer)
                        print('您现在有'+gold+'金币')
                        gold = int(gold)
                    elif gold >1500 and answer =='2':
                        gold = gold - 1500
                        answer = int(input('请说出要翻der倍数:'))
                        gold_mang = 100 * answer
                        gold = gold + gold_mang
                        print('您现在有'+str(gold)+'金币')
                        gold = int(gold)
                    else:
                        print('没有这个道具,游戏结束')
                        gold = 10000
                elif answer =='False':
                    print('继续游戏')
                else:
                    print('非法输入')
                    gold = 10000

其实到这里我们就基本完成了,但如果只写到这里会有很多bug,不想看了也可以就玩这个版本

还剩了一些代码,我也懒得写了,直接完整代码

十一、完整代码

import random
import time
name = str(input('请输入用户名:'))
print('欢迎您,'+name)
try:
    age = int(input('请输入年龄:'))
except ValueError:
    print('非法输入')
    age = 30000
    gold = 10000
if age <10:
    gold = 500
    print('您的初始金币为500')
if age <20 and age >10:
    gold = 1000
    print('您的初始金币为1000')
if age >20 and age <30:
    gold = 1500
    print('您的初始金币为1500')
if age >30 and age <1000:
    gold = 200
    print('年龄过大,您的初始金币为200')
if gold !=10000:
    Game_start = str(input('是否开始游戏?(True或False)'))
    while gold !=10000:
        if Game_start =='True':
            list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
            while list_123 ==10:
                list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
            answer = str(input('请猜数(big或small):'))
            if list_123[0] + list_123[1] + list_123[2] >10:
                result = 'big'
                error_result = 'small'
            if list_123[0] + list_123[1] + list_123[2] <10:
                result = 'small'
                error_result = 'big'
            if answer ==result:
                print('您赢了!')
                gold = gold + 100
                print('随机数为'+str(list_123))
                print('您现在有'+str(gold)+'金币')
            elif answer ==error_result:
                print('您输了...')
                print('随机数为'+str(list_123))
                gold = gold - 100
                print('您现在有'+str(gold)+'金币')
            else:
                print('非法输入')
                gold = 10000
            if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000:
                answer = str(input('您现在可以购买道具,是否购买?(True或False):'))
                if answer =='True':
                    print('请说出要购买der道具')
                    print('*'*41)
                    print('*金币翻倍器[现有的]   金币翻倍器[得到的]*')
                    print('*     2000G                  1500G      *')
                    print('*'*41)
                    answer = str(input('请说出要购买der道具(金币翻倍器[现有的]说1,金币翻倍器[得到的]说2):'))
                    if gold >2000 and answer =='1':
                        gold = gold - 2000
                        answer = int(input('请说出要翻der倍数:'))
                        gold = str(gold * answer)
                        print('您现在有'+gold+'金币')
                        gold = int(gold)
                    elif gold >1500 and answer =='2':
                        gold = gold - 1500
                        answer = int(input('请说出要翻der倍数:'))
                        gold_mang = 100 * answer
                        gold = gold + gold_mang
                        print('您现在有'+str(gold)+'金币')
                        gold = int(gold)
                    else:
                        print('没有这个道具,游戏结束')
                        gold = 10000
                elif answer =='False':
                    print('继续游戏')
                else:
                    print('非法输入')
                    gold = 10000
            if gold ==0:
                print('金币没了,游戏结束')
                gold = 10000
        elif Game_start =='False':
            print('欢迎游玩,下次再见!')
            gold = 10000
        else:
            print('非法输入')
            gold = 10000
print('游戏结束,再见!')
time.sleep(10)

到此这篇关于python实战之90行代码写个猜数字的文章就介绍到这了,更多相关python猜数字内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python实现简单购物商城
May 21 Python
Python冒泡排序注意要点实例详解
Sep 09 Python
Python3爬虫爬取百姓网列表并保存为json功能示例【基于request、lxml和json模块】
Dec 05 Python
Python自动发送邮件的方法实例总结
Dec 08 Python
详解Python requests 超时和重试的方法
Dec 18 Python
分享8个非常流行的 Python 可视化工具包
Jun 05 Python
django 配置阿里云OSS存储media文件的例子
Aug 20 Python
Python操作qml对象过程详解
Sep 26 Python
Django更新models数据库结构步骤
Apr 01 Python
Tensorflow中的降维函数tf.reduce_*使用总结
Apr 20 Python
Python如何执行精确的浮点数运算
Jul 31 Python
Python djanjo之csrf防跨站攻击实验过程
May 14 Python
python实战之一步一步教你绘制小猪佩奇
Apr 22 #Python
python 破解加密zip文件的密码
python入门之算法学习
Apr 22 #Python
python使用XPath解析数据爬取起点小说网数据
Apr 22 #Python
python 实现德洛内三角剖分的操作
python 三边测量定位的实现代码
python如何读取.mtx文件
Apr 22 #Python
You might like
索尼SONY ICF-SW7600GR电路分析与改良
2021/03/02 无线电
php中global和$GLOBALS[]的分析之一
2012/02/02 PHP
解析yahoo邮件用phpmailer发送的实例
2013/06/24 PHP
php权重计算方法代码分享
2014/01/09 PHP
ThinkPHP访问不存在的模块跳转到404页面的方法
2014/06/19 PHP
关于ThinkPhp 框架表单验证及ajax验证问题
2017/07/19 PHP
php7函数,声明,返回值等新特性介绍
2018/05/25 PHP
Laravel 自带的Auth验证登录方法
2019/09/30 PHP
解决Laravel无法使用COOKIE和SESSION的问题
2019/10/16 PHP
javascript实现动态CSS换肤技术的脚本
2007/06/29 Javascript
javascript 特殊字符串
2009/02/25 Javascript
论坛里点击别人帖子下面的回复,回复标题变成“回复 24# 的帖子”
2009/06/14 Javascript
js delete 用法(删除对象属性及变量)
2014/08/24 Javascript
JS+CSS实现的蓝色table选项卡效果
2015/10/08 Javascript
详解js图片轮播效果实现原理
2015/12/17 Javascript
解析预加载显示图片艺术
2016/12/05 Javascript
JQuery中Ajax的操作完整例子
2017/03/07 Javascript
jQuery实现点击关注和取消功能
2017/07/03 jQuery
protractor的安装与基本使用教程
2017/07/07 Javascript
详解Webpack多环境代码打包的方法
2018/08/03 Javascript
JavaScript跳出循环的三种方法(break, return, continue)
2019/07/30 Javascript
Python urlopen 使用小示例
2008/09/06 Python
python单链表实现代码实例
2013/11/21 Python
Window环境下Scrapy开发环境搭建
2018/11/18 Python
检测tensorflow是否使用gpu进行计算的方式
2020/02/03 Python
Python基于Webhook实现github自动化部署
2020/11/28 Python
Rag & Bone官网:瑞格布恩高级成衣
2018/04/19 全球购物
趣天网日本站:Qoo10 JP
2019/09/18 全球购物
数据库方面面试题
2012/04/22 面试题
会计实习期自我鉴定
2013/10/06 职场文书
法人任命书范本
2014/06/04 职场文书
机关作风建设自查报告及整改措施
2014/10/21 职场文书
餐厅收银员岗位职责
2015/04/07 职场文书
幼儿园2016圣诞节活动总结
2016/03/31 职场文书
初中运动会闭幕词范本3篇
2019/12/09 职场文书
Ruby序列化和持久化存储 Marshal和Pstore介绍
2022/04/18 Ruby