为什么我的打印声明不起作用?

时间:2013-10-19 14:51:24

标签: python

我只是一个初学者,所以请耐心等待,我的if和print声明有问题。有3种选择,A,B或C,这是A:

g = 0

ge = ("Gold =")

gh = ("The amount of gold you have is:")

choice3 = input()

if choice3 == "A":
      print("You slide him the coins through the bars.")(g = g - 5)(gh,g)("'Thanks!' He says. You manage to break out with him and escape to New Mexico, Well done, you win!")

这是我收到的错误消息:

A
You slide him the coins through the bars.
Traceback (most recent call last):
  File "F:\Program Files (x86)\Python\Python stuff\Hello World.py", line 111, in   <module>
print("You slide him the coins through the bars.")(g = g - 5)(gh,g)("'Thanks!' He says.     You manage to break out with him and escape to New Mexico, Well done, you win!")
TypeError: 'NoneType' object is not callable

2 个答案:

答案 0 :(得分:5)

这是因为print在调用后始终返回None。见下文:

>>> print(print('Hi'))
Hi
None
>>>

通常,Python会忽略此None。但是,确实存在,因为Python中的所有函数都必须返回某些内容

此外,在这一部分:

print("You slide him the coins through the bars.")(g = g - 5)

您尝试像函数一样调用None返回的print,并为其指定参数g = g - 5

请记住,Python中的函数是通过将(...)放在它们之后调用的。

Hereprint的参考资料,我认为可以帮助您。

答案 1 :(得分:1)

您不应该使用这样的打印,但请使用format,例如shown here

text_is = 'amazing'
print('Your text is {}'.format(text_is))

在您的情况下,可能是:

if choice3 == "A":
    g = g -5
    print("... {} {} {} 'Thanks!' .. you win!".format(g, gh, g))