用我的f-string修复这段代码?

时间:2017-04-04 15:51:25

标签: python python-3.x f-string

我看了this

有这个代码

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.

2 个答案:

答案 0 :(得分:2)

我看到你正在关注LPTHW教程,我strongly建议你选择一个不同的教程,因为你当前的教程有一些非常有趣的意见和{ {3}}。

回到你的问题:你需要将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只是调用函数时,它将返回并打印元组。

答案 1 :(得分: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)