-2

我只是一个初学者,所以请耐心等待,我的 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
4

2 回答 2

5

这是因为print总是None在它被调用后返回。见下文:

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

通常,这None会被 Python 简单地忽略。但是,它确实存在,因为 Python 中的所有函数都必须返回something

此外,在这部分:

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

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

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

print我认为对您有帮助的参考。

于 2013-10-19T14:54:53.750 回答
1

你不应该这样使用 print ,而是使用format例如,显示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))
于 2013-10-19T15:01:46.143 回答