首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python: TypeError:+:'int‘和'function’不支持的操作数类型。操作返回变量有困难吗?

Python: TypeError:+:'int‘和'function’不支持的操作数类型。操作返回变量有困难吗?
EN

Stack Overflow用户
提问于 2014-09-15 02:08:47
回答 3查看 6.5K关注 0票数 0

我正在学习编程,我正在做一个个人项目来补充我的课堂作业--一个测试,也可以帮助我记住我的德语词汇。现在,我很难弄清楚如何让这个东西自己评分。下面是我想要修改的代码:

代码语言:javascript
运行
复制
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

我的完整错误如下:

代码语言:javascript
运行
复制
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'**

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

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

EN

Stack Overflow用户

发布于 2014-09-15 03:26:32

请告诉我您使用的是哪个版本的Python,并对您的代码进行注释。请解释一下为什么要这样写代码。

这是带注释的修订版,可以在Python2.7中使用。

代码语言:javascript
运行
复制
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

下面是不带注释的代码:

代码语言:javascript
运行
复制
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
票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25836403

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档