2

My code passes all tests of check50 running check50 cs50/2018/x/sentimental/mario/more except this one:

rejects a height of 24, and then accepts a height of 2 expected " # #\n## ##\n", not ""

I have checked multiple times myself and each time it rejects 24 and shows the correct output for 2, yet I cannot figure out why check50 cannot detect the correct output!

Here's my code:

while True:
    h = int(input("Height: "))
    if h >= 0 and h < 24:
         break

for i in range(h):

    # printing blank spaces in the same row
    for w in range(h - (i + 1)):
        print(" ", end="")

    # printing blocks in the same row
    for b in range(i + 1):
        print("#", end="")

    print("  ", end="")

    # printing blocks in the same row
    for b in range(i + 1):
        print("#", end="")

    # moving down to the next row
    print()

screenshot proof

Here's the check50 log

I cannot figure out what is wrong with my code here or is it a bug in check50?

1

Combination of two mistakes, one on your end, one on their end.

  1. The tester seems pass an empty line between the 24 and 2. I'm not sure how this can happen, and it happens only for people using input or cs50.get_string, not for those using cs50.get_int.

    That way, it indirectly compensates for taking a crash for a rejection in the last two tests (empty string or a word). That's where I consider the real problem with the tester, it isn't strict enough at that end.

  2. Your programme crashes whenever the input string does not represent an integer. Try input some random word or empty string.

    There are a few approaches how to fix this. One would be to test the input line for consisting of digits only. The other would be to use a try-except block around, like

    while True:
        try:
            h = int(input("Height: "))
            if h >= 0 and h < 24:
                break
        except ValueError:
            pass     # do nothing, keep the loop going
    

    BTW, Python unlike C allows you to chain comparison operators, like if 0 <= h <= 23:

| improve this answer | |
  • Addressing your second point first, yes I know I haven't done exception handling here, but it in no way affects the result of my check50 test, which is my question and only concern here. Anyway thank you for letting me know about the chain comparison operators, I didn't know that. About the first point, if it is true I sincerely hope the cs50 team notices this issue and fixes this, because it is so unfair to get 85% without my fault! [PS: Any upvotes to this question may bring this to their notice :P ] – Kewal Shah Jun 10 '18 at 15:23
  • I just put my snippet into your code, and check50 shows all green. – Blauelf Jun 10 '18 at 15:34
  • Yes, it did work, thank you, but my question is why did that solve the problem? I mean logically it should have passed the cs50 test even without it. In your own answer you have mentioned "for people using input" this problem occurs, so I hope this bug gets fixed – Kewal Shah Jun 10 '18 at 16:26
  • If your code passed the last two tests gracefully (without crashing), then the extra line wouldn't matter. If you're wondering why get_int isn't affected, it does multi-step error handling internally (see github.com/cs50/python-cs50/blob/develop/src/cs50/… ) – Blauelf Jun 10 '18 at 18:30
  • Yes but my point is the check50 test should be something like rejects non-numeric value like in the next few codes, instead of rejects a height of 24, and then accepts a height of 2 expected " # #\n## ##\n", not "", because my code fulfills the latter condition, and thanks for sharing the link to the internal error handling of get_int – Kewal Shah Jun 11 '18 at 4:26

You must log in to answer this question.

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