我是python的新手,我正在卡住打印将要打印的功能:
"墙上挂着99瓶啤酒99瓶啤酒 拿下一个并将它传递到墙上,98瓶啤酒 98瓶啤酒在墙上98瓶啤酒 拿下一个并将它传递到墙上,97瓶啤酒 墙上挂着97瓶啤酒,97瓶啤酒 拿下一个并将它传递给墙壁上的96瓶啤酒"
这是我的代码:
def sing(number):
number = 99
print(number, "bottles of beer on the wall", number, "bottles of beer")
print("Take one down and pass it around, ", end='')
number -= 1
print(number, "bottles of beer on the wall")
return number
print(sing(sing(sing(number))))
有人可以帮我解决我出错的地方吗?非常感谢它。
由于
答案 0 :(得分:3)
为什么不使用循环?您可以使用range
从number
向99
步骤1
。
def sing(number):
print(number, "bottles of beer on the wall", number, "bottles of beer")
print("Take one down and pass it around, ", end='')
print(number-1, "bottles of beer on the wall")
for number in range(99, 0, -1):
sing(number)
否则你可以在函数本身中添加一个循环
def sing(number):
while number > 1:
print(number, "bottles of beer on the wall", number, "bottles of beer")
print("Take one down and pass it around, ", end='')
number -= 1
print(number, "bottles of beer on the wall")
sing(99)
答案 1 :(得分:2)
您编写的代码问题在于,每次调用此函数时,您都需要重新定义number
,而不是使用您传入的值。您可以通过删除函数的第一行来修复它,并且可以删除最终的print
def sing(number):
print(number, "bottles of beer on the wall", number, "bottles of beer")
print("Take one down and pass it around, ", end='')
number -= 1
print(number, "bottles of beer on the wall")
return number
sing(sing(sing(99)))