Python 不会打印这个格式化的字符串。它在第二个引用之后立即抱怨语法错误。另一方面,当我返回相同的字符串时,它就很好了。我可能是愚蠢的,但这里有什么问题?
print '{0} lands by turn {1}: {2}'.format(lands, turn, land_drop_prob(lands, turn, lands=lands_in_deck, deck_size = 60))
如前所述,如果我将“print”更改为“return”,字符串返回就好了。
您的语法应该适用于 python 2.X。但是,在 python 3.X 中,print
语法发生了变化。你必须写print("blabla")
而不是print "blabla"
.
>>> print '{0} lands by turn {1}: {2}'.format("a", "b", "c")
File "<stdin>", line 1
print '{0} lands by turn {1}: {2}'.format("a", "b", "c")
^
SyntaxError: invalid syntax
>>> print('{0} lands by turn {1}: {2}'.format("a", "b", "c"))
a lands by turn b: c
>>>