0

I'm converting the more comfortable version of mario.c to Python3 and cannot get the following loop to work:

running = True
while running:
    print("Height: ", end="")
    height = cs50.get_int()

    if height < 0 or height > 23:
        running = False

I've tried the following too:

while True:
    print("Height: ", end="")
    height = cs50.get_int()

    if height < 0 or height > 23:
        break

1 Answer 1

1

The problem was in logic:

The loop wasn't actually breaking when I put in a correct value (between 0 and 23) hence the infinite loop, as the logic said for it to break only if it was less or greater. Changing the if statement to the following has fixed the issue:

if height > 0 and height < 23:
    break

If that rule isn't followed, the user will continuously be asked for an input, which is the desired function.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .