So I am trying to make my code take a person's desired character and make a pyramid with a desired base. But when I execute the program, the output never ends and no characters ever output.
This is the following code:
pyramid = str()
charnum = int(1)
num2 = 0
num3 = 0
char = input("Input character to make pyramid: ")
numb = input("Input what the base number has to be: ")
if int(numb) % 2 != 0:
numb2 = int(numb)
numb2 = (int(numb2) - 1) / 2
while int(numb2) != -1:
print(pyramid)
pyramid = ("")
numb3 = int(numb2)
numb2 = int(numb2) - 1
charnum2 = int(charnum)
charnum = int(charnum) + 2
while int(numb3) != 0:
pyramid += " "
numb3 = int(numb3) - 1
while int(charnum2) != 0:
pyramid += char
charnum2 = int(charnum2) - 1
Any help would be much apreciated
答案 0 :(得分:0)
in your third while loop, you are checking the value of charnum2, but not modifying it ever, you only modify charnum. Since the value of charnum2 never changes if it was no 0 to begin with the loop will run forever.
Edit: after looking through your code some more, it looks like you now have all the parts there, just the indentation of your Questions code is off. If your actual source is working at this point then you can ignore this but, it looks like you should indent some of you while loops so everything looks like:
pyramid = str()
charnum = int(1)
num2 = 0
num3 = 0
char = input("Input character to make pyramid: ")
numb = input("Input what the base number has to be: ")
if int(numb) % 2 != 0:
numb2 = int(numb)
numb2 = (int(numb2) - 1) / 2
while int(numb2) != -1:
print(pyramid)
pyramid = ("")
numb3 = int(numb2)
numb2 = int(numb2) - 1
charnum2 = int(charnum)
charnum = int(charnum) + 2
while int(numb3) != 0:
pyramid += " "
numb3 = int(numb3) - 1
while int(charnum2) != 0:
pyramid += char
charnum2 = int(charnum2) - 1
This way the first while loop is run once for each line of the pyramid, and the following two add the correct characters in the pyramid.