为什么我在全局变量编辑时遇到Type错误?

时间:2015-01-20 23:53:25

标签: python easygui

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

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

我无法弄清楚它为什么会发生。我相信这是我尝试调用和更改的全局变量,但我无法弄清楚如何阻止错误。我知道我的代码现在很乱,我正在尝试进行概念验证。

#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()

3 个答案:

答案 0 :(得分:3)

在第4行,您有MathType = "Addition"
在第18行,您有def MathType():

错误告诉您它无法调用字符串 MathType()实际上是MathType = "Addition",它是一个字符串,而不是一个函数。

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

答案 1 :(得分:1)

您有两种类型的&#39; MathType&#39;,一个是字符串,另一个是函数。

答案 2 :(得分:0)

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