0

What is wrong with my second For Loop (spaces)? It seems like it only runs one time, according to the first line of the pyramid.

import cs50

while True:
    print("Type a number between 0 and 23: ", end="")
    n = cs50.get_int()
    if n > 0 or n < 23:
        break
i=0
spaces = n - 1
for i  in range(i,n,+1):
    for spaces  in range(spaces,0,-1):
        print(" ", end="")
    spaces = spaces - 1
    print("#" * (i+2), end="")
    print()

Results

Type a number between 0 and 23: 5
    ##
###
####
#####
######
1

I don't think it's a good idea to name the loop variable spaces when that name is already taken; that's probably where it goes wrong.

Also, why don't you multiply the spaces like you did with the hashes?

And you need the end="" only when you don't want the newline character to be added at the end. For the last part of the line, you actually want a newline. I think I did that one with one print call per line, combining the strings of spaces and hashes.

| improve this answer | |
  • 1
    You are right. Thanks. Removing the loop from the spaces and printing spaces just like the hashes now it works! – user13767 Feb 1 '17 at 1:29

You must log in to answer this question.