Python 我需要输入一个数字。如果否,则显示“否”;请输入数字“;然后再次请求输入

Python 我需要输入一个数字。如果否,则显示“否”;请输入数字“;然后再次请求输入,python,python-3.x,input,integer,Python,Python 3.x,Input,Integer,这就是我想要的,但是对于整数。我不允许使用中断或继续退出循环 # 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'"

这就是我想要的,但是对于整数。我不允许使用
中断
继续
退出循环

# 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: ')