Python 循环错误,因此测验';s的数学查询会自动重复

Python 循环错误,因此测验';s的数学查询会自动重复,python,math,error-handling,Python,Math,Error Handling,我有一段基本上是测验的代码。我有错误处理,所以如果有人按下字母或数字以外的任何东西,它将继续进行测验,而不是让该人尝试另一个问题。(例如,如果问题5是2+3,并且他们输入了t,那么它将继续进行,并且不会为问题5给出不同的问题)。 我试图更新编码,因此它会循环: def random_question():#defines function to get random question count = 0#equates count to 0 for number in range (0,10):

我有一段基本上是测验的代码。我有错误处理,所以如果有人按下字母或数字以外的任何东西,它将继续进行测验,而不是让该人尝试另一个问题。(例如,如果问题5是2+3,并且他们输入了t,那么它将继续进行,并且不会为问题5给出不同的问题)。 我试图更新编码,因此它会循环:

def random_question():#defines function to get random question
count = 0#equates count to 0
for number in range (0,10):#makes the code generate the question 10 times
    try:#the code makes it so the computer tries one thing and is the beggining
        questionList = [random_sum,random_subtraction,random_times]#puts functions in a list to be called on
        randomQuestion = random.choice(questionList)#calls on the list to generate the random questions
        randomQuestion()#calls on variable
        except ValueError:#if there is a value error or a type error it will notice it and try to stop it
            print ("Please enter a number")#prints out the statement if there is a type error or a value error
            else:
                break
random_question()#calls on the function random_question
但是,它会出现语法错误,并突出显示ValueError旁边的“Exception”部分


非常感谢您提供有关这一点的任何帮助。

您的except语句应该与try语句具有相同的缩进。在示例代码中,它缩进了一个额外的选项卡,这将导致该错误

Python非常重视缩进,它常常是罪魁祸首。我不清楚您的def行是否是占位符,或者此代码是否是其中的一部分,但如果此代码是函数定义的一部分,则需要担心其他缩进问题

我建议仔细检查一下,确保所有东西都排好了。基本的经验法则是,如果某个东西是另一个东西的一部分,它会缩进到它下面

def something():
    step 1
    step 2
    if (condition):
        the true thing
    else:
        the false thing
    while (something):
        repeat something
this is not part of the function anymore