1

I need your help. This is my program so far

import turtle
turtle.showturtle()

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 == 'quit' :
                  break
              elif i == 'new' :
                  turtle.reset()
              else :
                  continue

print('Control the turtle!')
turtle_interface()

As you can see, when the string has no [n] after it, it's being ignored. How can I fix this?

unor
  • 92,415
  • 26
  • 211
  • 360
  • Is `raw_input` what you really want here, http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x ? – Bi Rico Nov 15 '12 at 01:37
  • 1
    Given the `print()` function call at the bottom, they are using the appropriate `input()` for python 3. – wim Nov 15 '12 at 01:41
  • Ah yes, it even says in the link I provided that raw_input in 2.x is the same as input in 3.x, sometimes it's hard for me to think outside my bubble. – Bi Rico Nov 15 '12 at 02:08

1 Answers1

3

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'.

Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148