我一直在努力找出如何调用函数,有人可以解释我在这个例子中做错了什么


#create function with two paramaters
def quiz_item(question,solution):

#ask question and get input

question = input("The blue whale is the biggest animal to have ever lived. ")
solution = "T"
#while loop
while question != solution:
question = input("INCORECT try again: The blue whale is the biggest animal to have ever lived. ")
#if answer is == to solution print correct
if question == solution:
print("That is correct!")
break
quiz_item(question,solution)

#output表示解决方案未定义

我应该如何调用函数来运行

看起来你还在学习。那很好啊!

非常好。你会有多种解决方案,但我会试着解释其中最简单的一个。

最简单的解决方案:您可以定义该函数没有参数。这样的:

#create function with two paramaters
def quiz_item():

#ask question and get input     
question = input("The blue whale is the biggest animal to have ever lived. ")
solution = "T"
#while loop
while question != solution:
question = input("INCORECT try again: The blue whale is the biggest animal to have ever lived. ")
#if answer is == to solution print correct
if question == solution:
print("That is correct!")
break
quiz_item()

您已经在内部定义了变量'question'和'solution'函数,因此在调用函数时不需要将它们作为参数传递。

你的代码应该是这样的:

#create function with two paramaters
def quiz_item():
# ask question and get input
question = input("The blue whale is the biggest animal to have ever lived. ")
solution = "T"
# while loop
while question != solution:
question = input("INCORECT try again: The blue whale is the biggest animal to have ever lived. ")
#if answer is == to solution print correct
if question == solution:
print("That is correct!")
break

if __name__ == '__main__':
quiz_item()