Python 3.4:TypeError:“str”对象不是callab

2024-04-25 14:33:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python做一个基本的游戏,我试图在一个不同的函数中调用一个全局变量。 这是我收到的错误消息:

File "C:\ARENA\Arena.py", line 154, in <module>
    gamea()
  File "C:\ARENA\Arena.py", line 122, in gamea
    if age1 > age2():
TypeError: 'str' object is not callable

我对Python还是个新手,所以我不知道怎么了。这是我想修改的代码部分

    #character Titles Player1
def char1Title():
    print ("Player 1")
    print()
    global  myName1
    myName1 = input("Whom might you be?")
    print()
    global age1
    age1 = input("What is your age?")

#2 player gameplay code
def gamea():
    attack = input('Enter to start BATTLE!!!!')
**#here's where I try to call "age1" again:**
    if age1 > age2():
        print(myName)
        print("WINS!!")
    elif age2 > age1():
        print(myName2)
        print("WINS!!")

Tags: inpyinputifisdeflineglobal
1条回答
网友
1楼 · 发布于 2024-04-25 14:33:08

调用字符串时,不要在结尾处加括号,否则python会认为它是一个函数:

>>> string = "hello"
>>> string()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>> string
'hello'
>>> 

这是您编辑的代码:

    #character Titles Player1
def char1Title():
    print ("Player 1")
    print()
    global  myName1
    myName1 = input("Whom might you be?")
    print()
    global age1
    age1 = input("What is your age?")

#2 player gameplay code
def gamea():
    attack = input('Enter to start BATTLE!!!!')
    if age1 > age2:
        print(myName)
        print("WINS!!")
    elif age2 > age1:
        print(myName2)
        print("WINS!!")

另外,您不会在任何地方调用age2,因此请确保调用。

相关问题 更多 >