Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用f字串帮我修复这个代码?_Python_Python 3.x_F String - Fatal编程技术网

Python 用f字串帮我修复这个代码?

Python 用f字串帮我修复这个代码?,python,python-3.x,f-string,Python,Python 3.x,F String,当我读书的时候 有这个密码 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_

当我读书的时候

有这个密码

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.

我看到您正在学习《LPTHW教程》,我强烈建议您选择不同的教程,因为您当前的教程有一些非常有趣的观点和建议

回到您的问题:您需要解包
secret\u formula()
调用:

b, j, c = secret_formula(start_point)

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

f-strings
基本上只是将变量放入字符串中以调用它,而且由于
secret\u formula()
返回一个元组,因此当您仅调用函数时,它将返回并打印元组

我想把这段代码修改成f-string

打印“我们也可以这样做:”
打印“我们将有%d个豆子、%d个罐子和%d个板条箱。”%secret\u公式(起点)

你有关于python 3的书给我吗?如何用f-string打印这段代码?`````打印({0:{11}.format('hello'))``@kunjun的方式与使用
.format()
的方式相同:
print(f'{x:{11}')