Python TypeError:NoneType 对象不可调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28418329/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:15:11  来源:igfitidea点击:

TypeError: NoneType object is not callable

python

提问by Lonto81

I'm new to Python and I can't figure out what's wrong with this code. Everything works except that instead of printing out my print statement I get this error instead, ""and your favorite color is %s")(name, quest, color) TypeError: 'NoneType' object is not callable" Here's my code below.

我是 Python 新手,无法弄清楚这段代码有什么问题。一切正常,除了我没有打印出我的打印语句,我得到了这个错误,""你最喜欢的颜色是 %s")(name, quest, color) TypeError: 'NoneType' object is not callable" 下面是我的代码。

name = input("What is your name?")
quest = input("What is your quest?")
color = input("What is your favorite color?")

print ("Ah, so your name is %s, your quest is %s,"
"and your favorite color is %s")(name, quest, color)

采纳答案by Ryan Haining

Your string format syntax is wrong

您的字符串格式语法错误

print("Ah, so your name is %s, your quest is %s and your favorite color is %s" % (name, quest, color))

Though you may prefer the newer .formatstyle

虽然你可能更喜欢较新的.format风格

print("Ah, so your name is {}, your quest is {} and your favorite color is {}".format(name, quest, color))

Or, as of Python3.6 you can use f-strings

或者,从 Python3.6 开始,您可以使用 f-strings

print(f"Ah, so your name is {name}, your quest is {quest} and your favorite color is {color}")


The error you are receiving is due to the evaluation of print. Given the error I'm assuming you're using python3 which is doing something like this

您收到的错误是由于对 的评估造成的print。鉴于错误,我假设您正在使用 python3,它正在做这样的事情

print('hello')()

This is evaluated as

这被评估为

(print('hello'))()

which will call print with the argument 'hello'first. The printfunction returns Noneso what happens next is

这将'hello'首先使用参数调用 print 。该print函数返回None所以接下来发生的事情

(None)()

Noneis not callable, hence your error

None不可调用,因此您的错误