0

What is the problem with this simple code? I'm new with Python

Also I know Python doesn’t have a do while loop, so instead I use while True, but break, or stop the loop, if n < 0 or n > 23:

import cs50

def main():
    height = get_int()

def get_int():
    while True:
         n = input("Type a number between 0 and 23: ", end="")
         if n < 0 or n > 23:
    break
return n

if __name__ == "__main__":
    main()

Gives me this error

~/workspace/pset6/ $ python mario.py
  File "mario.py", line 10
    break
    ^
IndentationError: expected an indented block
1

Python is very nit–picking about how you indent your code. In your case, the break should have 8 more spaces before it. return n should have 4 more. Like bellow:

import cs50

def main():
    height = get_int()

def get_int():
    while True:
        n = input("Type a number between 0 and 23: ", end="")
        if n < 0 or n > 23:
            break

    return n

if __name__ == "__main__":
    main()

You should also reconsider the condition for the break. Are you sure you want to break when the number is out of bounds?

You must log in to answer this question.