我需要输入数字。如果没有,请显示请输入号码并再次要求输入

时间:2015-05-04 06:26:25

标签: python-3.x

这是我想要的,但对于整数。我不允许使用BREAK或CONTINUE退出循环感谢

$#basically i need this, but with an int(input('please enter a numb

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]? ')

1 个答案:

答案 0 :(得分:0)

您可以尝试将字符串转换为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: ')