0

My program

import turtle
i = "input()"
while i == "input()":
    if i == "exit":
        input()
    if i == "fd":
        turtle.fd(1)
    if i == "bk": 
        turtle.bk(1)
    if i == "lt":
        turtle.lt(90)
    if i == "rt":
        turtle.rt(90)
    if i == "pu":
        turtle.pu()
    if i == "pd":
        turtle.pd()

Now the file that is my input is (fd,fd,fd,bk,bk,lt,rt etc) it is suppose to draw a square but nothing shows up what am i doing wrong

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
user2089413
  • 1
  • 1
  • 1

3 Answers3

3

In Python there is a function you can call, input(). But you are not calling it. You put quotes around it, so you are just referencing a string that contains the letters 'i', 'n', 'p', 'u', 't', '(', ')'.

Remove the quotes so you actually call the input() function.

EDIT: From your comment below, it looks like you are using Python 2.x; so you should use raw_input(). raw_input() just returns whatever string the user typed; input() tries to evaluate it as a value.

x = input()  # if user types "2", x is set to the number 2
x = raw_input()  # if user types "2", x is set to the string "2"

EDIT: You need to make sure the pen is down, you probably want the turtle to go more than just 1 when going forward or back, and you need to make the screen appear.

I suggest you read a basic intro to turtle graphics in Python.

http://www.blog.pythonlibrary.org/2012/08/06/python-using-turtles-for-drawing/

Before the start of the while loop, try putting this:

screen = turtle.getscreen()

That should be enough to make the graphics screen pop up.

Good luck and have fun.

steveha
  • 74,789
  • 21
  • 92
  • 117
3

The line while i == input(): (even after removing the quotes) is not assigning a value to i. It is comparing the result of input() to i, which is probably not what you intend.

MatthewD
  • 2,509
  • 2
  • 23
  • 27
1

As the others have said, you want to take away the quotes around the function, not every single quote:

import turtle
while True:
    i = input()
    if i == "exit":
        break # stops the loop
    if i == "fd":
        turtle.fd(1)
    if i == "bk": 
        turtle.bk(1)
    if i == "lt":
        turtle.lt(90)
    if i == "rt":
        turtle.rt(90)
    if i == "pu":
        turtle.pu()
    if i == "pd":
        turtle.pd()

You got the NameError because if you took the quotes off of "fd", you have a variable, fd, not a string. However, the variable fd has not been defined, therefore you get an error.

However, if you are using Python 2.x (not 3.x) you should use raw_input() (this returns a string):

import turtle
while True:
    i = raw_input()
    if i == "exit":
        break
    if i == "fd":
        turtle.fd(1)
    if i == "bk": 
        turtle.bk(1)
    if i == "lt":
        turtle.lt(90)
    if i == "rt":
        turtle.rt(90)
    if i == "pu":
        turtle.pu()
    if i == "pd":
        turtle.pd()
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94