In Python, is it possible to skip a certain part of a for loop?
<p>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</p>

<pre><code>   x
  xxx
  xMx
 xxxxx
</code></pre>

<p>instead of </p>

<pre><code>   x
  xMx
 xxxxx
</code></pre>

<p>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.)</p>

<pre><code>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 &gt; 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
</code></pre>

<p>Thank you!</p>

<p>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:</p>

<pre><code>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 &gt; 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
</code></pre>

python loops