简体   繁体   中英

CS50 Mario More Sentimental Python - Not Printing Rows

I am attempting to write the CS50 Mario More Sentimental (Python) and I am running across some issues. The code is not performing correctly and is generating some issues as listed below:

Results for cs50/problems/2020/x/sentimental/mario/more generated by check50 v3.1.2
:) mario.py exists.
:) rejects a height of -1
:) rejects a height of 0
:( handles a height of 1 correctly
    expected ""#  #"", not ""
:( handles a height of 2 correctly
    expected "" #  #"\n"##  ...", not ""
:( handles a height of 8 correctly
    expected ""       #  #"\...", not ""
:( rejects a height of 9, and then accepts a height of 2
    expected program to reject input, but it did not
:) rejects a non-numeric height of "foo" 
:) rejects a non-numeric height of "" 
To see the results in your browser go to https://submit.cs50.io/check50/74938be07d19fd3664e32b052c21717012088526

I am not understanding why it will not display the blocks now...

# make sure there is valid input
while True:
    try:
        # ask for input
        height = int(input("Height: "))

        # make sure height is greater than 0 and less than or equal to 8
        if height >= 1 and height <= 8:
            break

        # iterate through height
        for counter in range(height):
            print(" " * (height - 1 - counter), end="")
            print("#" * (counter + 1), end="")
            print(" " * 2, end="")
            print("#" * (counter + 1), end="")
            print(end="\n")

    # display error message is value entered is below or above 1 and 8
    except ValueError:
        print("Please enter a number between 1 and 8. ", end="\n")

Not sure why we need while loop at all. Try taking height input from user without while loop and check if h>=1 & h<=8, if yes: run your for loop else: print error message & exit()

height = int(input("Height: "))
if height >= 1 and height <= 8:
        for counter in range(height):
            print(" " * (height - 1 - counter), end="")
            print("#" * (counter + 1), end="")
            print(" " * 2, end="")
            print("#" * (counter + 1), end="")
            print(end="\n")
else:
 print("Please enter height between 1 and 8")
 exit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM