英文:
Python function keeps missing required argument, despite being written
问题
我试图让一个按钮传递一个函数,并已经添加了将要使用的变量(以避免使用全局变量),然而,尽管我已经编写了它,但我一直得到一个缺少位置参数的错误。
def ShowQuestion(quiz, instance, questionsAnswered, questionsdone, question, user_answer, check_answer, data):
user_answer.focus_set()
if instance == 0:
question_set = random.sample(data, 5)
questionask = random.choice(question_set)
current_question = questionask['question']
question.config(text=current_question)
question_set.remove(questionask)
check_answer.config(command=lambda: Question.Checkanswer(quiz, instance, questionsAnswered, questionsdone, question, user_answer))
check_answer.grid(row=5, column=3)
def Checkanswer(quiz, choicer, instance, questionsAnswered, questionsdone, question, user_answer):
#print(choicer)
questionsdone += 1
if type(user_answer.get()) == str:
user_answer = str(user_answer.get()).lower()
else:
user_answerwrong = user_answer.get()
if user_answer in question_answer:
correct_question += 1
else:
pass
if questionsAnswered == 5:
messagebox.showwarning("Final Score", "Game Over \n Final Score: %s \n" %(self.user_score))
quiz.destroy()
else:
instance += 1
user_answer.delete(0, 'end')
Question.ShowQuestion(quiz, instance, questionsAnswered, questionsdone, question, user_answer, check_answer, data)
英文:
I am trying to have a button pass a function, and have put in the variables that will be used (to avoid globals), however, I keep getting the error that I am missing a positional argument, despite the fact that I have already written it.
def ShowQuestion(quiz, instance, questionsAnswered, questionsdone, question, user_answer, check_answer, data):
user_answer.focus_set()
if instance == 0:
question_set = random.sample(data,5)
questionask = random.choice(question_set)
current_question = questionask['question']
question.config(text=current_question)
question_set.remove(questionask)
check_answer.config(command = lambda : Question.Checkanswer(quiz, instance, questionsAnswered, questionsdone, question, user_answer))
check_answer.grid(row = 5, column = 3)
def Checkanswer(quiz, choicer, instance, questionsAnswered, questionsdone, question, user_answer):
#print (choicer)
questionsdone += 1
if type(user_answer.get()) == str:
user_answer = str(user_answer.get()).lower()
else:
user_answerwrong = user_answer.get()
if user_answer in question_answer:
correct_question +=1
else:
pass
if questionsAnswered == 5:
messagebox.showwarning("Final Score","Game Over \n Final Score: %s \n" %(self.user_score))
quiz.destroy()
else:
instance +=1
user_answer.delete(0, 'end')
Question.ShowQuestion(quiz, instance, questionsAnswered, questionsdone, question, user_answer, check_answer, data)
答案1
得分: 2
在这个 lambda
函数调用的参数列表与函数定义之间存在不匹配 (6 个参数):
check_answer.config(command = lambda : Question.Checkanswer(quiz, instance, questionsAnswered, questionsdone, question, user_answer))
和函数定义 (7 个参数):
def Checkanswer(quiz, choicer, instance, questionsAnswered, questionsdone, question, user_answer):
你忽略了 choicer
参数。要么传递它,要么从函数定义中删除这个参数(因为在函数体中没有使用它)。
注意:没有提供你的其余代码,我不确定如何解释 Question
对象——它是一个模块、某种方式引用本地文件范围还是一个类。因此,我假设 Question.Checkanswer
是指给定的函数 Checkanswer
。
英文:
There is a mismatch between the argument list in the function called by this lambda
function (6 arguments):
check_answer.config(command = lambda : Question.Checkanswer(quiz, instance, questionsAnswered, questionsdone, question, user_answer))
and the function definition (7 arguments):
def Checkanswer(quiz, choicer, instance, questionsAnswered, questionsdone, question, user_answer):
You skipped over the choicer
argument. Either pass it or remove the argument from the function definition (seeing as you don't use it in the function body).
Note: Without the rest of your code, I'm not sure how to interpret the Question
object—whether it is a module, somehow referring to the local file scope, or a class. So I assumed that Question.Checkanswer
refers to the given function Checkanswer
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论