0

My python code works correctly, outputting the appropriate number of spaces and #s to the terminal window, but it fails check50 for one test:

:( rejects a height of 24, and then accepts a height of 2
    expected " # #\n##  ##\n", not " h = int(inp..."

I can see that it has to do with my user input line, in which I used:

h = int(input("Height: "))

in a makeshift do-while loop, breaking if the integer is between 0-23. I understand that I can, instead, use and import get_int() from the cs50 library which passes check50 successfully, but why exactly doesn't my initial implementation using input() work?

1 Answer 1

1

The output shown makes no sense to me, but int will throw an error if the value is not the representation of an integer.

You can for example use it like

while True:
    user_input = input("Height: ")
    # isdigit returns True only if all characters are digits, AKA non-negative integer
    if user_input.isdigit():
        h = int(user_input)
        if 0 <= h <= 23:
            break

(any way of avoiding nested if and repeated int(user_input) at same time?) or wrap a try..except around the line.

You must log in to answer this question.

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