0

I'm stuck again! I've tried to port mario into python but I'm totally stuck on the first line! I'm trying to use 'cs50.get_int()' as shown but my programme doesn't prompt for the number, then if I put the number (x) in anyway (at the blank cursor) then it just shows 'Height: x' and closes....help?!!!!! My code is:

`import cs50

def main():

    while True:
        i = cs50.get_int()
        print("Height: {}".format(i))

        #if input is correct
        if i > 1 or i < 24:
            break
        #print the hashes
            x = i + 1

            for k in range (x):
                a = x - k
                print(' ' * a, end="")
                print('#' * k)
                print('#' * x)

if __name__ == "__main__":
    main()`
0

Just like in c, get_int() waits for the user to enter input with no prompt. If you want one, you have to put it in yourself, and probably want to do that first. When the first line is get_int(), that's exactly what it's going to do! After it's got an int from the user, it prints Height: i, where i is the int that was just entered, just like you describe. If the number is a valid integer, it quits the loop. What happens if it's not an integer between 2 and 23? Maybe you have too much code in your loop ;)

Remember that only the code in a loop should be indented, there are no curly braces to demarcate where loops start and end here. Think about it in the same terms as you did in c, what needs to be in the loop and under what conditions you can proceed to draw the pyramid!

Code does exactly what you tell it to in the order that you tell it to do it every time.

2
  • yayyy!! I got it! Thank you! I'd completely forgotten the logic and was just copying it from the c, and I'd also forgotten we hadn't actually learned how to prompt yet! I was assuming the print statement was the prompt...I so was rushing and not thinking...my bad! I never thought I'd say I missed c but I like curly braces!!!! And until you said to think about it I didn't think through the logic of what needed to be inside the loop and how unindenting the last line would take it out of the loop! Steep learning curve - thanks again!!!! :-)
    – CallyB
    Feb 21 '17 at 22:26
  • No problem, glad it worked for you! Feb 22 '17 at 6:09

You must log in to answer this question.

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