so I have absolutely no experience with coding. I've been trying to solve the mario less exercise from week 1 on CS50 but my hashtags keep printing in reverse order, so I get an inverted pyramid. I've tried looking for solutions online but I can't understand how it works. I've also tried changing values here and there, but since I don't understand how I'm telling the machine to print a symbol X times, I'm obviously not doing the right changes. The changes I can remember at the moment are setting g to 23 and then decreasing g, I've tried doing h+1 or h-1. So what's the logic behind it? How do I get it to start printing two #s and then increasing its value? So my code is:


And what that produces is:

inverted pyramid


EDIT! I changed the code and now it'll print the hashtags in the right way BUT it'll go back to doing it backwards IF I enter an invalid number first. Please help.


new code

And what happens is:

executed code

Last edit (if anyone's curious) I changed the first lines of the code to the suggested do while loop. It's all good now.

enter image description here

  • I'm curious though, where did you get the concept to write it this way - that resulted in an upside down pyramid? I've been seeing a rash of these lately and I'm wondering if there's something that's leading people to this.
    – Cliff B
    Commented Jul 11, 2018 at 22:47
  • Well since the first part (the equation for spaces) worked, I figured I wanted the exact same opposite to happen for hashtags, so I tried changing the "h-1" for a "h+1", or instead of setting the g to 0 (which is the minimum amount of spaces it'll print) I set g to 24, and so on. So basically trying to do the exact opposite.
    – TotalNoob
    Commented Jul 12, 2018 at 21:10

1 Answer 1


The number of spaces are based on the total number of lines and the current line number ( which combine to be the number of lines remaining), and decreases from line to line, but not the hashes.

Hashes are only dependent on the current line number and increase from one line to the next, never dependent on the total number of lines. The code above prints hashes based on the total number of lines at the start, h, (or number of lines remaining) which decreases. With each loop, the total number of hashes printed on a line, g, will decrease, thus the upside down pyramid.

A rethink is in order. You may get both right immediately, or you may find that fixing the number of hashes will break the number of spaces. I'll let you have a crack at reworking the code to see if you can do it on your own first.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • I thought of what you said and I came to the conclusion I was setting my condition for the hashtag's loop to continue, to the value I needed it ti stop it, or the last line it's supposed to print. It works for most cases EXCEPT when I enter an invalid number and then enter a valid one. It goes back to printing the pyramid backwards and I have no idea why. I'm adding those new screenshots to the original ask.
    – TotalNoob
    Commented Jul 12, 2018 at 21:15
  • The logic for getting the height isn't right. It's always going to ask for the height twice. Try replacing it with a single request inside a do/while loop and see what that does first. After that, the code is still written to base the number of hashes on the number of rows remaining.
    – Cliff B
    Commented Jul 12, 2018 at 21:26

You must log in to answer this question.

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