需要Python递减方面的帮助吗
好的!我得到的代码不能正常工作,下面是代码:需要Python递减方面的帮助吗,python,Python,好的!我得到的代码不能正常工作,下面是代码: def abDucks (a,b): while (a>1): print(a, "Little Ducks swimming in the sea") print("And if ",b," Little Ducks should accidentally drown") print("There'll be ", (a-b), "Little
def abDucks (a,b):
while (a>1):
print(a, "Little Ducks swimming in the sea")
print("And if ",b," Little Ducks should accidentally drown")
print("There'll be ", (a-b), "Little Ducks swimming in the sea\n")
a=(a-b)
if (a<=1):
print(a, "Little Ducks swimming in the sea")
print("And if ",b," Little Ducks should accidentally drown")
print("There'll be no Little Ducks swimming in the sea\n")
您可以像这样简洁地编写相同的代码
def abDucks (a,b):
while (a - b >= 1):
print(a, "Little Ducks swimming in the sea")
print("And if ",b," Little Ducks should accidentally drown")
print("There'll be ", (a-b), "Little Ducks swimming in the sea\n")
a -= b
else:
print(a, "Little Ducks swimming in the sea")
print("And if ",b," Little Ducks should accidentally drown")
print("There'll be no Little Ducks swimming in the sea\n")
通过此更改,输出变为
请用正确的缩进编辑文章。请记住,Python中的缩进非常重要。我回滚了编辑。林奇,让奥普自己做缩进。我们可以做出一个很好的猜测,但我更愿意看看实际的缩进是什么。谢谢,伙计,这真的帮了大忙,我从昨天开始就一直试图弄清楚这一点,但我不知道发生了什么。非常感谢你!
def abDucks (a,b):
while (a - b >= 1):
print(a, "Little Ducks swimming in the sea")
print("And if ",b," Little Ducks should accidentally drown")
print("There'll be ", (a-b), "Little Ducks swimming in the sea\n")
a -= b
else:
print(a, "Little Ducks swimming in the sea")
print("And if ",b," Little Ducks should accidentally drown")
print("There'll be no Little Ducks swimming in the sea\n")
8 Little Ducks swimming in the sea
And if 2 Little Ducks should accidentally drown
There'll be 6 Little Ducks swimming in the sea
6 Little Ducks swimming in the sea
And if 2 Little Ducks should accidentally drown
There'll be 4 Little Ducks swimming in the sea
4 Little Ducks swimming in the sea
And if 2 Little Ducks should accidentally drown
There'll be 2 Little Ducks swimming in the sea
2 Little Ducks swimming in the sea
And if 2 Little Ducks should accidentally drown
There'll be no Little Ducks swimming in the sea