如何获得正确的号码?

How to get correct number?

--Python 这段代码有什么问题?

a=int(input('enter a number'))
b=list(range(1,11))
if a not in b:
   int(input('enter a number'))
else :
   print('ok')
    

输出:

 enter a number 89
 enter a number 8

请进一步解释一下您要做什么,如果您尝试为“8”的第二个输入输出“OK”,那么您需要更改此代码的逻辑。

a=int(input('enter a number'))

此行打印“输入数字” 然后你的 if 语句检查它是否在 b 内 但如果不是,它会再次打印“输入数字”而不对其进行任何操作,如下一行所示:

int(input('enter a number'))

如果您希望代码 运行 通过 if-else 块不止一次,请尝试使用 for 循环或 while 循环。

  • 另外,请注意您没有再次将输入放入变量 a。

编辑:试试这个代码:

    print('enter a number')
a=int(input())
while a not in range(1,11):
    print('enter a number')
    a=int(input())
else :
   print('ok')