user3624023
user3624023

Reputation: 1

How to iterate through the string completely in python?

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

Upvotes: 0

Views: 67

Answers (1)

Jiminion
Jiminion

Reputation: 5168

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)

Upvotes: 3

Related Questions