在函数中捕获用户输入时出错

时间:2017-08-08 08:54:15

标签: python input

我一直在努力使用此功能来捕获用户输入中的错误。

def user_choice():
            choice = 4
            while choice > 3
                    choice = int(input("what list is your card in"))
            return choice

任何想法将不胜感激。 感谢

1 个答案:

答案 0 :(得分:0)

这可以通过使用您想要的有效参数实现一个数组来完成,然后使用in关键字检查它是否存在。

def user_choice():
        choice = int(input('what list is your card in?: '))
        valid = [1,2,3]
        if choice in valid:
            return choice
        else: 
            print('oops! must be between 1-3 please try again')
            user_choice()