Python 3.4:类型错误:“str”对象不可调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23691532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python 3.4: TypeError: 'str' object is not callable
提问by user3643088
I'm making a basic game in python and I'm trying to recall a global variable in a different function. This is the error message I get:
我正在用 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
I'm still new to Python so I'm not sure what's wrong. Here's the part of my code that I'm trying to fix
我还是 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!!")
采纳答案by A.J. Uppal
When you call a string, don't put parentheses around the end, otherwise python will think it is a function:
调用字符串时,末尾不要加括号,否则python会认为它是一个函数:
>>> string = "hello"
>>> string()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>> string
'hello'
>>>
Here is your edited code:
这是您编辑的代码:
#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!!")
Also, you don't call age2
anywhere, so make sure you do.
此外,你不会age2
在任何地方打电话,所以一定要打电话。