【发布时间】:2020-06-22 08:07:06
【问题描述】:
我正在编写一个简单的程序,它可以在墙上写出 99 瓶啤酒的歌词。但是,当我在 while 循环中调用它时,我编写的从 BottleNum 中减一的函数不起作用。我不确定我在这里做了什么?我不应该能够在while循环中调用函数吗?运行代码时没有出现错误,文本反复打印,bottlesNum 始终等于 99。
bottlesNum = 99
def bottle_subtraction(bottles):
bottles = bottles - 1
while bottlesNum > 0:
if bottlesNum != 1:
print("{x} bottles of beer on the wall, {x} bottles of beer".format(x = bottlesNum))
elif bottlesNum == 1:
print("{x} bottle of beer on the wall, {x} bottle of beer".format(x = bottlesNum))
bottle_subtraction(bottlesNum)
print("Take one down and pass it around, {} bottles of beer on the wall".format(bottlesNum))
【问题讨论】:
-
"难道我不能在 while 循环中调用函数吗?"你确实在调用它。但是,我不确定您为什么希望调用它会影响全局
bottlesNum。 -
你的函数基本上什么都不做,你只是将
bottle - 1的结果分配给一个局部变量 -
你在用python 3吗?
标签: python function while-loop