0

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!

Dqnny
  • 11
  • 4
  • 5
    Use `continue` instead of `break`. `break ` exits the loop, while `continue` just skips the rest of the current iteration and then continues (hence the name). – Efferalgan Oct 05 '16 at 05:59
  • I think you are looking for `continue` to skip the current iteration of the loop – OneCricketeer Oct 05 '16 at 05:59
  • @Efferalgan So where would I put it? I would assume at the start of "if x == textrow" but when I do that I just get a regular output as if there was no text – Dqnny Oct 05 '16 at 06:06
  • 2
    Please, refer to [Example use of “continue” statement in Python?](http://stackoverflow.com/questions/8420705/example-use-of-continue-statement-in-python) and [Break and Continue in Loops](http://stackoverflow.com/documentation/python/237/loops/875/break-and-continue-in-loops#t=201610050606378050317). – Andrii Abramov Oct 05 '16 at 06:06
  • If you put the loop contents in a function you can `return` early. – Peter Wood Oct 05 '16 at 06:14
  • `testrow = height / 2` should be put outside the loop, you only need to define it once. – Efferalgan Oct 05 '16 at 06:17

1 Answers1

0

I am not sure of how how your code works, but I tweaked it until it returned the result I expected. Also, I made small changes to parts that were a bit ugly. This should do what you want:

def Triangle(height, text):
number_of_x2 = (height - len(text)) / 2

if not height % 2:
    print "Please enter a height that is odd"

else:
    string_triangle = "x"
    ht = "x" * (2 * height - 1)

    length_of_ht = len(ht) / 2 
    indent_text_length = length_of_ht / 2
    text_row = height / 2

    # should be height change later
    for x in range(height):
        if x == 1:
            string_triangle = " " * length_of_ht + string_triangle
            print string_triangle

        if x > 1:
            for x in range(height):
                if x == text_row - 1:
                    string_triangle += "xx"
                    string_triangle = string_triangle[1:]
                    continue

                if x == textrow:
                    special_line = " " * (number_of_x2 + len(text) / 2) + "x" * number_of_x2 + text + "x" * number_of_x2

                    if not len(text) % 2:
                        special_line += "x"

                    print special_line

                if string_triangle == ht or len(text) == len(string_triangle):
                    break

                string_triangle += "xx"
                string_triangle = string_triangle[1:]

                print string_triangle
Tomasz Dzieniak
  • 2,765
  • 3
  • 24
  • 42
Efferalgan
  • 1,681
  • 1
  • 14
  • 24