0

I am learning python and I would like the turtle to move according to the string that I have input. If I only have one character in my string, the turtle will move. However, if I have more than two characters in my string, my turtle will not move at all. Here is my code:

 import turtle

 wn = turtle.Screen()

 crystal = turtle.Turtle()
 crystal.speed(0)

 def instructions(string):
     for char in string:
         if char in string == "F":
             crystal.forward(100)

         elif char in string == "+":
             crystal.right(60)

         elif char in string == "X":
             print ("X is an invalid command")

 instructions("F+F")

 wn.exitonclick()

1 Answer 1

3

You don't need to say char in string twice. After the for, just use char.

Like:

for char in string:
    if char == "F":
        crystal.forward(100)
Sign up to request clarification or add additional context in comments.

2 Comments

Also, in the future naming variables 'string' and 'char' may end up being problematic, as keywords are sometimes the same names.
neither "char" nor "string" is a builtin type or keyword.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.