Search code examples
pythonpython-3.xpython-turtle

If last item printed on screen by a list in python turtle = value1 then move to a different position and print value 2


Trying to see if its possible to do the following using python turtle graphics (assume modules have been imported already):

my_values = ['value1', 'value2', 'value3'] #these values are strings 



def heading():
  if my_values == 'value1':
    setheading(90)
    forward(10)
  if my_values == 'value2':
    setheading(0)
    forward(30)
  if my_values == 'value3':
    setheading(270)
    forward(60)

def working_together():
 for loop in range(3):
    write(my_values[value_range], move = False, align = 'center')
    heading()

working_together()

The main premise of what im trying to achieve is that if the item last printed on the screen by turtle == valueX then the turtle will then move to a different position. This must loop through until the loop is complete and all values have been printed?

Is this possible in python?

Ive tried the following:

def heading():
  if range(my_values == 'value1'):
    setheading(90)
    forward(10)
  if range(my_values == 'value2'):
    setheading(0)
    forward(30)
  if range(my_values == 'value3'):
    setheading(270)
    forward(60)

def heading():
  if range(len(my_values == 'value1')):
    setheading(90)
    forward(10)
  if range(len(my_values == 'value2')):
    setheading(0)
    forward(30)
  if range(len(my_values == 'value3')):
    setheading(270)
    forward(60)

Solution

  • Pass it as an argument to heading().

    def heading(current_value):
      if current_value == 'value1':
        setheading(90)
        forward(10)
      elif current_value == 'value2':
        setheading(0)
        forward(30)
      elif current_value == 'value3':
        setheading(270)
        forward(60)
    
    def working_together():
        for value in my_values:
            write(value, move = False, align = 'center')
            heading(value)
    
    working_together()