相关疑难解决方法(0)

如何将用户输入限制为仅Python中的整数

我正在尝试进行多项选择调查,允许用户从1-x选项中进行选择.我怎样才能这样做,如果用户输入除数字之外的任何字符,返回"这是一个无效的答案"之类的东西

def Survey():
    print('1) Blue')
    print('2) Red')
    print('3) Yellow')
    question = int(input('Out of these options\(1,2,3), which is your favourite?'))
    if question == 1:
        print('Nice!')
    elif question == 2:
        print('Cool')
    elif question == 3:
        print('Awesome!')
    else:
        print('That\'s not an option!')
Run Code Online (Sandbox Code Playgroud)

python-3.x

1
推荐指数
2
解决办法
4万
查看次数

标签 统计

python-3.x ×1