鱼C论坛

 找回密码
 立即注册
查看: 2957|回复: 8

[萌新报道] 新人疑惑

[复制链接]
发表于 2020-3-31 16:08:43 | 显示全部楼层 |阅读模式

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

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

x
各位请看一下,这里的while not 语句这里是出问题了吗,按理来说输入整数的话就会退出第一个循环呀,为什么我输入整数和其他格式都会进入第一个循环呢?
这是代码:
import random
secert = random.randint(1,10)
print("-----你在想什么----")
times = 2
temp = input("不妨猜一下我在想什么:")
while not isinstance (temp , int):
    print('not lawful')
    temp = input('please give an integer:')
while ((temp != secert) and (times > 0)):
    if temp > secert:
        print("大了,大了")
    else:
        print("小了,小了")
    times = times - 1
    temp = input("错了,请重新输入吧:")
    while not isinstance (temp , int):
        print('not lawful')
        temp = input('please give an integer:')
if temp == secert:
    print("我草,厉害,没有奖励")
    print("游戏结束")
else:
    print('again?')
运行结果:
-----你在想什么----
不妨猜一下我在想什么:5
not lawful
please give an integer:
not lawful
please give an integer:大
not lawful
please give an integer:2.0
not lawful
please give an integer:3
not lawful
please give an integer:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-31 16:20:44 | 显示全部楼层
input返回的一定是字符串,改为
  1. import random
  2. secert = random.randint(1,10)
  3. print("-----你在想什么----")
  4. times = 2
  5. temp = input("不妨猜一下我在想什么:")
  6. while not temp.isdigit():
  7.     print('not lawful')
  8.     temp = input('please give an integer:')
  9. temp=int(temp)
  10. while ((temp != secert) and (times > 0)):
  11.     if temp > secert:
  12.         print("大了,大了")
  13.     else:
  14.         print("小了,小了")
  15.     times = times - 1
  16.     temp = input("错了,请重新输入吧:")
  17.     while not isinstance (temp , int):
  18.         print('not lawful')
  19.         temp = input('please give an integer:')
  20. if temp == secert:
  21.     print("我草,厉害,没有奖励")
  22.     print("游戏结束")
  23. else:
  24.     print('again?')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

发表于 2020-3-31 16:15:02 From FishC Mobile | 显示全部楼层
感觉重复使用了循环麻烦又复杂,跟Python的简洁不挂钩了,哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-31 16:19:45 | 显示全部楼层
非零即一 发表于 2020-3-31 16:15
感觉重复使用了循环麻烦又复杂,跟Python的简洁不挂钩了,哈哈

哈哈,您这么一说也的确是哦
但总觉得在逻辑上过得去却不能实现就很奇怪呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-31 16:31:56 | 显示全部楼层
永恒的蓝色梦想 发表于 2020-3-31 16:20
input返回的一定是字符串,改为

嗯嗯,懂了,原来问题是出在input函数这块了。
实在谢啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-31 16:33:08 | 显示全部楼层
小楼昨夜又细雨 发表于 2020-3-31 16:31
嗯嗯,懂了,原来问题是出在input函数这块了。
实在谢啦

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

使用道具 举报

发表于 2020-3-31 16:34:49 | 显示全部楼层
小楼昨夜又细雨 发表于 2020-3-31 16:31
嗯嗯,懂了,原来问题是出在input函数这块了。
实在谢啦

emmm算了,你这不是提问帖
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-31 16:41:17 | 显示全部楼层
永恒的蓝色梦想 发表于 2020-3-31 16:34
emmm算了,你这不是提问帖

哈哈哈,我刚才还去百度了下怎么给最佳。新人,新人,嘻嘻
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-3 09:34:58 | 显示全部楼层
要转成整型
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-30 23:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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