Laurent
Laurent

Reputation: 31

Python Turtle prompt

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.

Upvotes: 0

Views: 242

Answers (2)

In Hoc Signo
In Hoc Signo

Reputation: 495

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 >=.

Upvotes: 0

Limina102
Limina102

Reputation: 1087

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

Upvotes: 1

Related Questions