0

我只是在学习编码,我正在做一个个人项目来补充我的课业——这个测验也可以帮助我记住我的德语词汇。现在我很难弄清楚如何让这个东西本身分级。这是我要修改的代码:

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'**

所有这些关于定义分数的废话都变得有点冗长了。我可能会考虑将其设置为作为模块运行。我也想尝试将其转换为提供 % 价值反馈,但我想我可以自己处理这些问题。现在,我宁愿在使代码复杂化之前解决这个问题。

有没有人可以帮我解决这个问题?我一直潜伏在论坛上,确实发现了另一个标题类似的问题,但我认为我们的问题没有类似的解决方案。

4

3 回答 3

0

请告知您使用的 Python 版本并注释您的代码。请解释你为什么这样写代码。

这是带有注释的修订版本,可在 Python 2.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-14T19:26:31.733 回答
0

我想,先试试这段代码。如果您使用 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))
于 2014-09-14T19:27:49.320 回答
0

看来您正在将该方法分配给一个变量,而不是实际调用该方法。在您的示例score = dialogue中,应替换为
score = dialogue(question,response,score)

于 2016-05-10T09:19:26.857 回答