1

当我读到这个

有这个代码

We can also do that this way:
We'd have 500000 beans, 500 jars, and 5 crates.

当我修改为 pep 498

print ("We can also do that this way:")
print (f"We'd have {secret_formula(start_point)} beans, {secret_formula(start_point)} jars, and {secret_formula(start_point)} crates.")

它打印这个

We can also do that this way:
We'd have (500000, 500, 5) beans, (500000, 500, 5) jars, and (500000, 500, 5) crates.
4

2 回答 2

2

我看到你正在关注LPTHW教程,我strongly建议你选择一个不同的教程,因为你目前的教程有一些非常有趣的观点和其他一些问题

回到您的问题:您需要将secret_formula()调用解包为:

b, j, c = secret_formula(start_point)

print (f"We'd have {b} beans, {j} jars, and {c} crates.")

f-strings基本上只是将变量放在字符串中以调用它,并且由于secret_formula()返回一个元组,当您 fstring 只是调用该函数时,它将返回并打印元组。

于 2017-04-04T15:55:21.693 回答
0

这段代码我想修复成 f-string print "We can also do that this way:" print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)

于 2017-04-06T02:56:02.227 回答