1

OK! I got this code that does not work properly, Here is the code:

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") 

The problem I have is that it counts to 0 and then it will print: And if no little ducks should accidentally drown There'll be no Little Ducks swimming in the sea

Please Help

I never did get an infinite loop lol :P This is what happens when I run this code using (8,2)

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 0 Little Ducks swimming in the sea 

0 Little Ducks swimming in the sea 
And if 2 Little Ducks should accidentally drown 
There'll be no Little Ducks swimming in the sea
2
  • 3
    Please edit the post with the correct indentation. Remember that indentation in Python is extremely important. Commented Mar 15, 2014 at 6:35
  • 2
    I rolled back the edit. Lynch, let OP do the indenting his or herself. We can make a really good guess, but I'd prefer to see what the actual indentation is. Commented Mar 15, 2014 at 6:42

1 Answer 1

3

You could write the same code, succinctly, like this

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")

With this change, the output becomes

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man, this really helped out a lot, Ive been trying to figure this out since yesterday and did not understand what was happening. Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.