so I attempted to add some code to my working product(A triangle maker) where you can add some text to the middle of the triangle if the program allows so. So far I've added the text with the correct amount of x's and indents but the program still outputs the original middle line of x's. For example Triangle(3, "M") would output
x
xxx
xMx
xxxxx
instead of
x
xMx
xxxxx
which is what I would like. I have tried using break when x == textrow - 1 but that did not seem to work and I think what I would have to do is somehow break the loop, print the user's text with the x's skip the part where the computer would normally print the xxx's and keep going. But I have no idea on how to do that. Here is my code, the only really relevant parts are under the "if x > 1" (I know that it is probably the most inefficient thing you have ever seen, but I'm a beginner and I would do what I understand.)
def Triangle(height, text):
text1 = str(text)
number_of_x2 = (height - len(text)) / 2
if height % 2 == 0:
print "Please enter a height that is odd"
else:
stringTriangle = "x"
HT = ""
for x in range(height + height - 1):
HT += "x"
length_of_HT = len(HT)
length_of_HT = int(length_of_HT)
length_of_HT = len(HT) / 2
length_of_HT = int(length_of_HT)
indent_text_length = length_of_HT / 2
for x in range(height): #should be height change later
if x == 1:
stringTriangle = " " * length_of_HT + stringTriangle
print stringTriangle
if x > 1:
textrow = height / 2
for x in range(height):
if stringTriangle == HT:
break
if len(text) == len(stringTriangle):
break
if x == textrow:
text = ""
text = " " * number_of_x2
for x in range(number_of_x2):
text+="x"
text = text + text1
for x in range(number_of_x2):
text+="x"
print text
stringTriangle += "xx"
stringTriangle = stringTriangle[1:]
print stringTriangle
Thank you!