![](/img/trans.png)
[英]I want to ask for user input again if the entered number doesnot consists in the matrix given?
[英]I need the input to be a number. If nor, display “please enter number” and ask for input again
这就是我想要的,但对于整数。 我不允许使用break
或continue
退出循环。
# basically I need this, but with an int(input('please enter a number'))
ask = input('Would you like to play Steal or Deal [y|n]? ')
while ask not in ('y', 'n'):
print ("Please enter either 'y' or 'n'")
print('')
ask = input('Would you like to play Steal or Deal [y|n]? ')
您可以尝试将字符串转换为 int 并捕获异常:
def is_number(s):
try:
int(s)
return True
except ValueError:
return False
ask = input('please enter a number: ')
while not is_number(ask):
print ("no, a number!")
print('')
ask = input('please enter a number: ')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.