用fstring为我修复这个代码?

2024-05-23 21:22:13 发布

您现在位置:Python中文网/ 问答频道 /正文

当我读this

有这个密码的

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

当我改成政治公众人物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.

Tags: andsecretthathavethisdostartcan
2条回答

我看到你正在遵循LPTHW教程,我strongly建议你选择一个不同的教程,因为你现在的教程有一些非常有趣的意见和some other issues。你知道吗

回到您的问题:您需要将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()返回一个元组,当你只是调用函数时,它会返回并打印元组。你知道吗

我想把这段代码改成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)

相关问题 更多 >