Reading two separate values in one line in python

后端 未结 1 948
轮回少年
轮回少年 2021-01-23 09:49

I need your help. This is my program so far

import turtle
turtle.showturtle()

def turtle_interface():
    while True :
          n = 0
          instructions =          


        
相关标签:
1条回答
  • 2021-01-23 10:27

    I think it is because of the if len(instructions) > 1: test. If the string has no [n] after it, then there will only be one instruction, and the length will not be greater than 1.

    You should try something like this:

    def turtle_interface():
        while True :
              n = 0
              instructions = input().split()
              i = instructions[0]
              if len(instructions) > 1:
                  n = int(instructions[1])
                  if i == 'forward' :
                      turtle.forward(n)
                  elif i == 'backward' :
                      turtle.backward(n)
                  elif i == 'left' :
                      turtle.left(n)
                  elif i == 'right' :
                      turtle.right(n)
              elif i == 'new' :
                  turtle.reset()
              elif i == 'quit' :
                  break
    

    Note the indentation and placement of the line for if i == 'new'.

    0 讨论(0)
提交回复
热议问题