首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么在编辑全局变量时会出现类型错误?

为什么在编辑全局变量时会出现类型错误?
EN

Stack Overflow用户
提问于 2015-01-21 07:53:25
回答 3查看 64关注 0票数 3

我正在创建一个easygui/python计算器数学程序,我一直收到这个错误

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\Python27\Scripts\MathHelper.py", line 83, in <module>
    MathType()
TypeError: 'str' object is not callable

我不明白为什么会这样。我相信是因为全局变量,我试图调用并修改它,但是我不知道如何停止这个错误。我知道我的代码现在有点乱七八糟的,我正在尝试一个概念的证明。

代码语言:javascript
复制
#MathType == what kind of math to compute. IE. Subtraction or addition
#Selection == Yes or no
math = 1
MathType = "Addition"
loop = 1
import easygui

def start():
  print("startMessage")
  MathType = easygui.msgbox(msg="Hello and welcome to my Math Helper.",
                 title = "Welcome")
  startMessage = "0"
#End of start
#
#
#

def MathType():
  global MathType
  print("Math Type Gathered")
  MathType = easygui.buttonbox("Select the type of Math you would like to compute:",
                                title = "Math Selection",
                                choices = ["Addition", "Subtraction", "Shut Down"] )
#End of MathType
#
#
#

def Addition():
  num1 = easygui.enterbox(msg = "Please enter the first Number.",
                     title = "Addition")
#print(num1)
  num2 = easygui.enterbox(msg = "Please enter the second number.  "+num1+" + ___ = ___",
                        title = "Addition")
#print(num2)
  easygui.msgbox("Here is your equation:  "+num1+"  +  "+num2+" = ___ ",
                 title = "Equation")
  NUM1 = int(num1)
  NUM2 = int(num2)
  numFinal = (NUM1 + NUM2)
  NUM3 = str(numFinal)

  easygui.msgbox(msg="Your answer is:  "+NUM3+"",
                 title="Final")
#print(numFinal)
#End of Addition
#
#

def Subtraction():
  num1 = easygui.enterbox(msg = "Please enter the first Number.",
                     title = "Subtraction")
#print(num1)
  num2 = easygui.enterbox(msg = "Please enter the second number.  "+num1+" - ___ = ___",
                        title = "Subtraction")
#print(num2)
  easygui.msgbox("Here is your equation:  "+num1+"  -  "+num2+" = ___ ",
               title = "Equation")
  NUM1 = int(num1)
  NUM2 = int(num2)
  numFinal = (NUM1 - NUM2)
  NUM3 = numFinal

  easygui.msgbox(msg="Your answer is:  "+NUM3+"",
               title="Final")
#print(numFinal)
#End of Subtraction
#
#

def MathFinder():
    if MathType == "Addition":
        print("Addition")
        Addition()
    elif MathType == "Subtraction":
        print("Subtraction")
        Subtraction()
    elif MathType == "Shut Down":
        exit()

start()
while loop == 1:
  MathType()
  MathFinder()
EN

回答 3

Stack Overflow用户

发布于 2015-01-21 07:57:31

在第4行,您有MathType = "Addition"

在第18行,您有def MathType():

该错误告诉您它不能调用字符串。

MathType()实际上是MathType = "Addition",它是一个字符串而不是一个函数。

请尽量避免对函数、变量等使用相同的名称。

票数 3
EN

Stack Overflow用户

发布于 2015-01-21 07:59:04

你有两种类型的'MathType',一种是字符串,另一种是函数。

票数 1
EN

Stack Overflow用户

发布于 2015-01-21 08:05:09

您有一个函数和一个名为MathType的字符串变量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28057349

复制
相关文章

相似问题

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