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):
and
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.