我正在尝试将结果打印到终端中,但f-string似乎无法正常工作,如下所示:
print(
f'Financial Analysis\n'
'----------------------\n'
'Total Months {monthnum}\n'
'Total: ${pl}\n'
'Greatest increase in profits: {greatim} ({greati}) \n'
'Greatest decrease in profits: {greatdm} ({greatd})'
)
Run Code Online (Sandbox Code Playgroud)
其余代码在此存储库中可用
在您的代码中,只有第一个字符串是f字符串。如果要插值,则需要将它们全部设为 f 字符串。
或者,您可以使用带有多行引号的 f 字符串(即''' ''')。这可能是一个更好的解决方案:
monthnum = 10
pl = "some pl"
greatim = "gim"
greati = 'gti'
greatdm = 'gdm'
greatd = 'gd'
print(f'''Financial Analysis
----------------------
Total Months {monthnum}
Total: ${pl}
Greatest increase in profits: {greatim} ({greati})
Greatest decrease in profits: {greatdm} ({greatd})'''
)
Run Code Online (Sandbox Code Playgroud)
印刷:
Financial Analysis
----------------------
Total Months 10
Total: $some pl
Greatest increase in profits: gim (gti)
Greatest decrease in profits: gdm (gd)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
237 次 |
| 最近记录: |