gpt4 book ai didi

python - 如何在字符串中打印函数?

转载 作者:太空宇宙 更新时间:2023-11-04 08:10:07 25 4
gpt4 key购买 nike

所以我尝试使用 Python 2.7 进行简单计算,代码如下:

print 'In SchmooLand everyone is 9.33356564 times heavier than on earth.\n'

weight = float(raw_input('How much do you weigh on Earth in pounds?\n'))

print 'In SchmooLand you would weigh'
print weight * 9.3356564
print 'in pounds'

虽然输出看起来像这样

How much do you weigh on Earth in pounds?
235423423
In SchmooLand you would weigh
2197832185.64
in pounds

如何在与字符串相同的行上打印权重计算的结果,以便像这样一起打印

In SchmooLand you would weigh 2197832185.64 in pounds

我完全是 n00b - 感谢您的帮助!

最佳答案

在每个打印行的末尾添加一个逗号以避免隐式换行:

print 'In SchmooLand you would weigh',
print weight * 9.3356564,
print 'in pounds',

来自print documentation :

A '\n' character is written at the end, unless the print statement ends with a comma.

关于python - 如何在字符串中打印函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23944651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com