0

this is my first time posting on here and I'm trying to not break any rules but sorry if I do.

So, I followed the specification to make mario.py ( they are the same ones for mario.c ) in Sentimental in Pset6. My program works for everything except the last two condition which I will post below

:( rejects a non-numeric height of "foo" expected program to reject input, but it did not :( rejects a non-numeric height of "" expected program to reject input, but it did not

I feel like my program's logic makes sense, it outputs "Error: type an integer" when the user inputs a non-numeric height, but that doesn't the check50.

Here is my code:

try :
    height = int(input("Height:"))
    while height < 0 or height > 23:
        height = int(input("Height:"))
except ValueError:
    print ("Error: type an integer")

else:
    for i in range((height-1),0,-1):
        j = i-1
        k = height-j
        print(' '*j,'#'*k)
    print('#'*(height+1))

I don't necessarily care too much about passing check50 because the rules aren't even that clear for this specific problem. It's just I'm on pset 6 which is the transition from C to Python I want to transfer my knowledge accurately.

I feel like this situation is implying I return an error (return -1 in C), is the way I did that the correct way to do so in python? Or are there different/ more pythonic ways to return an error ( reject the input )?

Thanks so much!

EDIT: ( Thanks @Blauelf Not sure if this is exactly what you meant but I appreciate the effort and I got it to work!)

while True:
    try:
        height = int(input("Height:"))
    except ValueError:
        pass
    else :
        break
while height < 0 or height > 23:
    height = int(input("Height:"))

if height==0:
    exit()

else:
    for i in range((height-1),0,-1):
        j = i-1
        k = height-j
        print(' '*j,'#'*k)
    print('#'*(height+1))
1

Remember your C code. Reject in this case means to ask again. Use a loop around the input, and leave it on valid input (via loop condition or break).

If the code were in a function, I'd raise an exception (or let an existing one pass without catching it). Or return None, but maybe not in this case.

You must log in to answer this question.

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