您好我正在尝试打印此字符串,但我收到错误。 我的代码出了什么问题?
def main():
for x in range (0, 100):
T = 100 - x
print (T+' bottles of beer on the wall,'+T+'bottles of beer.
'Take one down, pass it around,'+T-1+' bottles of beer on the wall.')
main()
错误是:
EOL while scanning the string literal
答案 0 :(得分:2)
有些事情是错的:
Take one down
之前输出中包含换行符。您必须包含明确的\n
换行符或使用单独的print
语句。更好的是,使用逗号而不是连接来使用print
语句内置功能:
print T, ' bottles of beer on the wall, ', T, ' bottles of beer.'
print 'Take one down, pass it around, ', T - 1, ' bottles of beer on the wall.'
但最好的选择是使用字符串格式:
print '{0} bottles of beer on the wall, {0} bottles of beer.'.format(T)
print 'Take one down, pass it around, {0} bottles of beer on the wall.'.format(T - 1)
答案 1 :(得分:0)
最好使用format
功能:
def main():
for x in range (0, 100):
T = 100 - x
print('{0} bottles of beer on the wall, {0} bottles of beer. '
'Take one down, pass it around, '
'{1} bottles of beer on the wall.'.format(T, T-1))