Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+7 votes

I got my bobbing fish and bubbles, but they both work their way out of the window after a while, for some reason. The fish slowly go up, and the bubbles slowly go to the left, and after a couple minutes, the window is empty. I think it has something to do with how my count function works, but I'm not sure. The following is a section from my move function in my fish class. In the setupFish function I set: count = random.randrange(0, 20)

if self.count == 20:
        self.count = 0
        
    if 20 > self.count > 10:
        dy = 1
    
    elif self.count <= 10:
        dy = -1
asked in CSC201 Spring 2021 by (1 point)

2 Answers

+4 votes

With help from Dr. Stonedahl, I was able to work this out: because dy was -1 when count was 0 - 10 and 1 when count was 11 - 19, it was more likely to be 1 than -1, so I needed to make it so when count was 10, dy was 1, so that dy was equally likely to be 1 or -1.

answered by (1 point)
+2 votes

First of all, the last elif statement wasn't necessary.
As when 'count' reaches 20, it will be reset to 0. The second if statement is for when 'count' is somewhere between 10 to 19. Therefore, you can clearly let dy = -1 outside of all the if/else statement because 'count' will always in between 0-9, if it does not meet the bool of the if statements.

Secondly, x <= 10 means that the range contains 10 numbers, while 20 > x > 10 only contains 8 numbers. Because of that, the likelihood of dy = -1 is more than dy = 1. That's why your fish and bubble slowly move outside of the window as dx and dy (respectively) have more chances of becoming negative over time.

Solution: On the second if statement, 'count' should be equal to 10 while the third statement shouldn't.

Thirdly, it seems to be an indentation error when you paste the code. Both the if statements don't relate to each other so you can dedent the second if statement.

answered by (1 point)
...