0

I'm building a turtle race game and I'd like to have a prompt asking for the user how many turtles he'd like to see racing.

    num_turtles = 0
    while 2 > num_turtles > 10:
        num_turtles = int(screen.textinput(title="How many racers...", prompt="How many turtles do you want to participate? (2-10)").lower())

The problem is that the loop is never entered. I want the game to keep asking while the number entered is not between 2 and 10.

Laurent
  • 31
  • 1
  • 5
  • 3
    The negation of `2 <= num_turtles <= 10` is not `2 > num_turtles > 10` since the latter is always false. You need `num_turtles < 2 or num_turtles > 10`. – John Coleman Jun 11 '21 at 12:50
  • 1
    Does this answer your question? [How to emulate a do-while loop?](https://stackoverflow.com/questions/743164/how-to-emulate-a-do-while-loop) – JeffUK Jun 11 '21 at 12:51
  • 2
    Damn I'm so stupid. I've used a AND at first in my statement instead of a OR. Then Pycharm suggested me this change to simplify but that's not clearer in my mind – Laurent Jun 11 '21 at 13:10

2 Answers2

1

2 > num_turtles > 10 equals to 2 > num_turtles and num_turtles > 10. To express 'the number entered is not between 2 and 10', try not (2 < num_turtles < 10).

user26742873
  • 919
  • 6
  • 21
0

You initially don't have enough turtles.

Your code's logic is "provided there are more than two turtles and less than ten, loop until it's false." The loop doesn't start if the condition is not true to begin with, which is the case here. You're looking for something like C's do-while loop, which unfortunately doesn't exist in Python.

To solve your problem, set num_turtles to 3.

Also, while I'm at it, you're going to get buggy behavior with your loop logic code. The way you have it currently, you must have more than two turtles. You might want to consider replacing the first > with >=.