Search code examples
pythonpython-turtle

I have had trouble creating a turtle code that the user can decide how far the turtle moves then how much they want it to turn and how fast it goes


As mentioned in the title I have been having probloms trying to create a turtle that the user can input how fast they want the turtle to move, how far they want the turtle to go forwards before turning and how much they want the turtle to turn before going forwards again. This is stage three of the code.

Stage one was allowing people to choose the amount it went forwards then how far they want it to turn through the code it's self then doing the distance forward and amount turned on repeat which I did by with this code.

import turtle

print ('Input the distance forward you want to go and the amount you want to turn, it will make a square by defalt')

while True:
    turtle.forward(50)
    turtle.left(90)

For stage two I made it so that the user can input the distance forward and amount turned from inside the code I did this by adding the variables d and t and make them define how far forwald the turtle went and how much it turned I did that with this code

import turtle

d = input("How far forward: ")
t = input("How much do you want to turn: ")

while True:
    turtle.forward(int(d))
    turtle.left(int(t))

I've been pretty good up to here but what I'm trying to do is a far bit harder since I've never touched a turtle until now and i can't seem to be able to set the speed of it no matter how I change the code btw I know how i have the code now is bad but I've been trying for about 4 days now so I just chucked in the last one i tryed but I had some far better ones and some worse ones then this

import turtle

e = int('0')
m = int('10')
f = int(input("How fast do you want to go: "))
d = input("How far forward: ")
t = input("How much do you want to turn: ")

if f < e:
    int(f = input("How fast do you want to go: "))
elif f > m:
    int(f = input("How fast do you want to go: "))

turtle.speed(int(f))

while True:
    turtle.forward(int(d))
    turtle.left(int(t))

Any help would be greatly appreciated so i can put this code to rest as it was just a thing I wanted to do so I could learn some a bit about how turtles work.


Solution

  • The line:

    int(f = input("How fast do you want to go: "))
    

    Should be an assignment of f, which casts the input. Not a cast of the assignment

    f = int(input("How fast do you want to go: "))
    

    You can force the user to input a value in a range by keeping them in a while True loop until they enter the correct value:

    import turtle
    
    MIN = 0
    MAX = 10
    
    while True:
        f = int(input("How fast do you want to go [0,10]: "))
        # If F is in the range of min and max, leave the loop
        if MIN <= f <= MAX:
            break
    
    d = int(input("How far forward: "))
    t = int(input("How much do you want to turn: "))
    
    
    turtle.speed(f)
    
    while True:
        # d is the length variable, this was f
        turtle.forward(d)
        turtle.left(t)