pythonloopsturtle-graphics

Is there a way to create a break when conditions are met?


import turtle

t = turtle

Width = int(t.numinput("Cartesain Coordinates", "Enter Width",200, minval = 200, maxval = 400))
Height = int(t.numinput("Cartesain Coordinates", "Enter Height", 200, minval = 200, maxval = 400))
Grid = t.textinput("Cartesian Coordinates", "Would you like a grid?")

# Defualt value for numinputs (x)

Default_width_value = 200

# List of possible (x) values

Possible_width_value1 = 250
Possible_width_value2 = 300
Possible_width_value3 = 350
Max_width_value = 400

# Starting the grid
if Grid == 'No':
    t.goto(0, 0)
if Grid == 'Yes':
    Grid = True
    while True:

        # The Outter Square
        t.penup()
        t.goto(-Width, +Height)
        t.pendown()
        t.goto(-Width, -Height)
        t.goto(+Width, -Height)
        t.goto(+Width, +Height)
        t.goto(-Width, +Height)

    # The Inner Lines For Width (x) portion of Grid
    # First Line
    
        t.goto(-Width, -Height)
        t.forward(50)
        t.pendown()
        t.left(90)
        t.forward(+Height)
        t.forward(+Height)
    
    # Second Line
    
        t.right(90)
        t.forward(50)
        t.right(90)
        t.backward(-Height)
        t.backward(-Height)
    

# Third Line

        t.left(90)
        t.forward(50)
        t.left(90)
        t.forward(+Height)
        t.forward(+Height)

    # Fourth Line
    
        t.right(90)
        t.forward(50)
        t.right(90)
        t.backward(-Height)
        t.backward(-Height)

    # Fifth Line

        t.left(90)
        t.forward(50)
        t.left(90)
        t.forward(+Height)
        t.forward(+Height)

    # Sixth Line

        t.right(90)
        t.forward(50)
        t.right(90)
        t.backward(-Height)
        t.backward(-Height)

    # Seventh Line

        t.left(90)
        t.forward(50)
        t.left(90)
        t.forward(+Height)
        t.forward(+Height)
        break

if Width > Default_width_value < Possible_width_value1:

        # First Line

        t.right(90)
        t.forward(50)
        t.right(90)
        t.backward(-Height)
        t.backward(-Height)

First id like to say that im very new to python

The issue is when creating a grid for a cartesian coordinate, i can get it to create the grid at the minval() but once i start increasing in increments of 50 till maxval() is where i have trouble. ive tried to put the if() in the while loop, which is where i found the problem, ive also tried it out side which is where it worked fine whilest increasing in incraments of 50 but the issue i had there was it would still try and draw part of the grid even if the user said no to the grid. Its for an assinment and i can only use whats in the turtle module. Any help is very much appreciated.


Solution

  • first of all, nice that you learn python! Currently your code doesn't need the break because you draw all the lines "by hand".
    In order to really need the break you need to loop through repeating lines of code that can be put together inside functions. I made some adaptions to the code that are also python-specific (p.E. import abbrevations, lower-case variable names):

    import turtle as t
    
    default_width_value = 200
    default_height_value = 200
    max_width_value = 400
    max_height_value = 400  
    
    width = int(t.numinput("Cartesain Coordinates", "Enter Width",200, minval = default_width_value, maxval = max_width_value))
    height = int(t.numinput("Cartesain Coordinates", "Enter Height", 200, minval = default_height_value, maxval = max_height_value))
    grid = t.textinput("Cartesian Coordinates", "Would you like a grid?")
    
    def draw_right_line_down():
        t.right(90)
        t.forward(50)
        t.right(90)
        t.backward(-height)
        t.backward(-height)
    
    def draw_left_line_up():
        t.left(90)
        t.forward(50)
        t.left(90)
        t.forward(+height)
        t.forward(+height)
    
    # Starting the grid
    if grid.lower() == 'no':
        t.goto(0, 0)
    elif grid.lower() == 'yes':
        # grid = True  # not needed
        # The Outter Square
        t.penup()
        t.goto(-width, +height)
        t.pendown()
        t.goto(-width, -height)
        t.goto(+width, -height)
        t.goto(+width, +height)
        t.goto(-width, +height)
    
        # The Inner Lines For width (x) portion of grid
        # First Line
    
        t.goto(-width, -height)
        t.forward(50)
        t.pendown()
        t.left(90)
        t.forward(+height)
        t.forward(+height)
    
        number_of_lines = 1
        max_number_of_lines = 7
    
        while number_of_lines < max_number_of_lines:
            if number_of_lines % 2 == 1:
                draw_right_line_down()
            else:
                draw_left_line_up()
            number_of_lines += 1
    
        # alternative with break:
        #while number_of_lines < max_number_of_lines:
        #    draw_right_line_down()
        #    number_of_lines += 1
        #    if number_of_lines == max_number_of_lines:
        #        break
        #
        #    draw_left_line_up()
        #    number_of_lines += 1
    else:
        raise ValueError("Invalid input. Please enter 'yes' or 'no'.")
    

    Hope that helps a bit and keep on practicing :)