'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 1:[1]

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)

Solution 2:[2]

I've found 5 major issues with your code:

  1. You did not instantiate a turtle object. What you are doing is just navigating the class itself, which probably brings forth quite some weird errors. After creating a turtle object you won't need the while True statement anymore either, which in general is not a good idea without a break statement to implement as it literally will run for forever.
  2. Your declaration of variables is really hard to read and makes it unnecessarily complicated. Note: If you need a statement only once, there is no need in assigning it any further name.
  3. The if clause can be simplified by leveraging the or operator.
  4. I don't know if I got your assignment right, but I think you really don't need a while loop here? If so you should consider breaking as soon as a square or rectangle is fully drawn.
  5. You can't assign a variable inside of the int() function unless you use the walrus operator :=
import turtle 

s = turtle.getscreen()
t = turtle.Turtle()

speed = -1
while speed < 0 or speed > 10:
  speed = int(input('How fast do you want the turtle togo?'))
forward = int(input('How far forward?'))
left = int(input('How far to the left?'))

t.speed(speed)
t.forward(forward)
t.left(left)

Didn't test the code, but the general idea should be helpful already. Cheers :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dominic K.