鱼C论坛

 找回密码
 立即注册
查看: 2548|回复: 5

猜数小游戏的改进问题

[复制链接]
发表于 2017-9-5 23:15:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import random
randomn=random.randint(0,100)
print("游戏开始")
secret=int(input('我正在想一个整数,它在1到100之间,猜一下:'))
count=1
while secret!=randomn and count<4:
    if secret<1 or secret>100 or isinstance(secret,int)==false:
        secret=int(input("好好看题:"))
        count=count+1
    else:
        if secret<randomn:
            print("小了小了")
        else:
            print("大了大了")
        secret=int(input("再猜一次:"))
        count=count+1

if secret!=randomn:
    print("很遗憾一次也没猜对")
else:
    print("你居然猜对了")


我的想法是在这个程序里输入的如果不是整数可以自动显示好好看题,但是好像没有达到预期效果,不知能否有大神解答一下,新手没币,如果能够解答甚是感谢。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-9-5 23:23:43 | 显示全部楼层
可以先把input的返回值转换为float,再用is_integer()方法判断是否为整数。
  1. >>> a=1.2
  2. >>> a.is_integer()
  3. False
  4. >>> b = 2.0
  5. >>> b.is_integer()
  6. True
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-5 23:26:00 From FishC Mobile | 显示全部楼层
try:
       secret=int(input('我正在想一个整数,它在1到100之间,猜一下:'))
except:
            print("好好看题")
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-5 23:26:05 | 显示全部楼层
冬雪雪冬 发表于 2017-9-5 23:23
可以先把input的返回值转换为float,再用is_integer()方法判断是否为整数。

感谢大神
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-5 23:31:01 | 显示全部楼层
左手十字 发表于 2017-9-5 23:26
try:
       secret=int(input('我正在想一个整数,它在1到100之间,猜一下:'))
except:

让我先去百度一下try except是个什么语句。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-5 23:54:32 | 显示全部楼层
  1. from random import randint
  2. number = randint(1,100)

  3. secret = 0

  4. print('GAME START')
  5. count  = 1
  6. while secret != number and count <4:
  7.       try:
  8.             secret = int(input('猜一个1-100之间的整数:'))
  9.       except:
  10.             print('好好看题')
  11.       finally:
  12.             if secret < number and secret !=0:
  13.                   print('小了')
  14.             elif secret > number:
  15.                   print('大了')
  16.             count +=1
  17. if secret != number:
  18.       print('很遗憾一次也没有猜对')
  19. else:
  20.       print('你居然猜对了')
  21.       
  22. print('GAME OVER')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-23 17:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表