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

huangapple go评论101阅读模式
英文:

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

问题

my_values = ['value1', 'value2', 'value3']  # 这些值是字符串

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()
英文:

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)

答案1

得分: 1

将其作为参数传递给 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()
英文:

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()

huangapple
  • 本文由 发表于 2023年3月23日 08:59:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定