I am working on mario.py and the code compiles, except when I run it, it keeps prompting the user for an integer and does not print out the pyramid.

enter image description here

I know that there is something incorrect with the for loop that I've written for printing out the pyramid. I tried to nest two while loops within a for loop except I'm not sure where the problem lies. enter image description here

I was wondering if anyone could tell me what is wrong with my code, I would really appreciate it.

1 Answer 1


First: DO NOT POST CODE AS IMAGES. Post as text, there's a {} button in the editor you can click after selecting the code, which will add four spaces in the front, which tell markdown to format as code. Surely is not nearly as beautiful (no line numbers for example), but one could copy that code snippet to own code and try it out. Or use a service like pastebin or GitHub's gist, but no images, please.

Also, it would have been great if you told us where and in which way the code behaves in an unexpected way. Just identifying and describing the problem and what the code is actually meant to do might help solving the problem yourself.

You are slightly wrong in implementing this one:

  • If the user fails to provide a non-negative integer no greater than 23, you should re-prompt for the same again.

Should be height >= 0 and height <= 23, as that's the condition to leave the loop. Also notice that 0 and 23 are valid values according to the task description.

Your while-loops with in-loop increment could be for-loops, which are much easier to read:

for spaces in range(height-i):


for hashes in range(i+1):

If you want to print single characters (you don't have to, Python supports simple string concatenation, which allows for a single print per line or even for all lines), you'd have to add , end="" to your print commands, just like you did for the first one, to prevent it from automatically printing a newline.

Python has some nice syntax for repeating strings or array content, so you wouldn't even need nested loops: "#"*(i+1) repeats "#" i+1 times.

  • Thank you for your helpful advice :). It took me a long time but I was eventually able to rewrite the code and get it working using for loops like you suggested. Commented Mar 9, 2017 at 11:18
  • Thanks for mentioning python's ability to repeat strings and array content. Maybe it was in lecture and it just didn't stick, but I had one of those epiphany moments when I heard Zamyla say "no more nested for loops" and I realized I had missed the boat.This explains it.
    – Lindsey
    Commented Oct 30, 2017 at 18:44

You must log in to answer this question.

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