我正在使用 python 计算如果每 7 秒有一个孩子出生,5 年内将出生多少个孩子。问题出在我的最后一行。当我在它的任一侧打印文本时,如何让变量工作?
这是我的代码:
currentPop = 312032486
oneYear = 365
hours = 24
minutes = 60
seconds = 60
# seconds in a single day
secondsInDay = hours * minutes * seconds
# seconds in a year
secondsInYear = secondsInDay * oneYear
fiveYears = secondsInYear * 5
#Seconds in 5 years
print fiveYears
# fiveYears in seconds, divided by 7 seconds
births = fiveYears // 7
print "If there was a birth every 7 seconds, there would be: " births "births"
原文由 Bob Uni 发布,翻译遵循 CC BY-SA 4.0 许可协议
打印时使用
,
分隔字符串和变量:,
在打印功能中用一个空格分隔项目:或更好地使用 字符串格式:
字符串格式化功能更强大,还允许您执行一些其他操作,例如填充、填充、对齐、宽度、设置精度等。
演示: