我正在学习编程,我正在做一个个人项目来补充我的课堂作业--一个测试,也可以帮助我记住我的德语词汇。现在,我很难弄清楚如何让这个东西自己评分。下面是我想要修改的代码:
def dialogue (question,response,score):
if question == response:
print ("Correct! ")
score = 1
else:
print ("Correct answer: " + response)
score = 0
return score
score = dialogue
currentScore = 0
currentScore = currentScore + score
question = raw_input ("Good morning ")
response = ("guten morgen")
dialogue(question,response,score)
print currentScore我的完整错误如下:
Traceback (most recent call last):
File "C:/Users/Burgess/Desktop/COLLEGE FOLDER/scoreMod.py", line 12, in <module>
currentScore = currentScore + score
**TypeError: unsupported operand type(s) for +: 'int' and 'function'**所有这些定义分数的废话都变得有点冗长了。我可能会考虑将其设置为一个模块。我也想尝试将其转换为%值反馈,但我想我可以自己处理这些问题。现在,我宁愿先解决这个问题,然后再使代码复杂化。
有没有人能帮我解决这个问题?我一直潜伏在论坛上,我确实找到了另一个具有类似标题的问题,但我不认为我们的问题有类似的解决方案。
发布于 2014-09-15 03:26:32
请告诉我您使用的是哪个版本的Python,并对您的代码进行注释。请解释一下为什么要这样写代码。
这是带注释的修订版,可以在Python2.7中使用。
def dialogue(question,response): # you are defining dialogue as a function
# This function checks whether the answer matches the correct answer
if question.lower() == response.lower(): # .lower() makes it not case sensitive
print ("Correct! ")
return 1 # If matches, return 1
else:
print ("Correct answer: " + response)
return 0 # If does not match, return 0
currentScore = 0 # Initial Score
question = raw_input("Good morning: ") #Asking for input
response = "guten morgen" #Correct answer
currentScore += dialogue(question, response) #calling the function with 2 arguments
#adding the returned score to currentScore
"""
currentScore += dialogue(question, response)
is same as
currentScore = currentScore + dialogue(question, response)
"""
print currentScore #returning output下面是不带注释的代码:
def dialogue(question,response):
if question.lower() == response.lower():
print ("Correct! ")
return 1
else:
print ("Correct answer: " + response)
return 0
currentScore = 0
question = raw_input("Good morning: ")
response = "guten morgen"
currentScore += dialogue(question, response)
print currentScore发布于 2014-09-15 03:27:49
我想,先试一下这段代码。如果你使用3.X版本,你不应该使用raw_input。如果您想比较两个句子,请尝试使用x.upper()。
def dialogue (question,response):
if question == response:
print ("Correct! ")
score = 1
else:
print ("Incorrect answer: " + response)
score = 0
return score
currentScore = 0
question = input("Good morning ")
question='Good morning'
response='guten morgen'
score = dialogue(question,response)
currentScore = currentScore + score
print ('Your score:',(currentScore))发布于 2016-05-10 17:19:27
您似乎是将该方法赋值给一个变量,而不是实际调用该方法。在您的示例中,score = dialogue应该被替换为
score = dialogue(question,response,score)
https://stackoverflow.com/questions/25836403
复制相似问题