小楼昨夜又细雨 发表于 2020-3-31 16:08:43

新人疑惑

各位请看一下,这里的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:

永恒的蓝色梦想 发表于 2020-3-31 16:20:44

input返回的一定是字符串,改为import random
secert = random.randint(1,10)
print("-----你在想什么----")
times = 2
temp = input("不妨猜一下我在想什么:")
while not temp.isdigit():
    print('not lawful')
    temp = input('please give an integer:')
temp=int(temp)
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?')

非零即一 发表于 2020-3-31 16:15:02

感觉重复使用了循环麻烦又复杂,跟Python的简洁不挂钩了,哈哈

小楼昨夜又细雨 发表于 2020-3-31 16:19:45

非零即一 发表于 2020-3-31 16:15
感觉重复使用了循环麻烦又复杂,跟Python的简洁不挂钩了,哈哈

哈哈,您这么一说也的确是哦{:5_109:}
但总觉得在逻辑上过得去却不能实现就很奇怪呢{:10_254:}

小楼昨夜又细雨 发表于 2020-3-31 16:31:56

永恒的蓝色梦想 发表于 2020-3-31 16:20
input返回的一定是字符串,改为

嗯嗯,懂了,原来问题是出在input函数这块了。
实在谢啦{:5_109:}

永恒的蓝色梦想 发表于 2020-3-31 16:33:08

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

不妨给个最佳{:10_254:}

永恒的蓝色梦想 发表于 2020-3-31 16:34:49

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

emmm算了,你这不是提问帖

小楼昨夜又细雨 发表于 2020-3-31 16:41:17

永恒的蓝色梦想 发表于 2020-3-31 16:34
emmm算了,你这不是提问帖

哈哈哈,我刚才还去百度了下怎么给最佳。新人,新人,嘻嘻

天假之名 发表于 2020-4-3 09:34:58

要转成整型
页: [1]
查看完整版本: 新人疑惑