Why isn't python working?

I got python idle up and running, but when I tried to code it with things like “import turtle” and then things like “forward(30)” and “right(90)” it wasn’t working and kept on saying things like “NameError: name ‘forward’ is not defined”. Does anyone know what is wrong with python, and how to fix it? Because I need to know how to use python for something quite urgent :frowning:

Use from turtle import * instead of import turtle.

2 Likes

Hello,

just by importing a package library does not mean that you have access to all of its attributes. If you use:

import turtle

this implies that you would have to prefix (also known as qualifying) every attribute with the library package name. In this case, turtle. For example, to make use of the action attribute forward, you would need to qualify it as shown here:

turtle.forward(30)

On the other hand, if you had imported the library package with an asterisk (*), then this implies that you are given access to all of its contents or namespace without restrictions and thus nullifying the need for qualifying any of its variables (i.e., constants, attributes, methods, classes, etc.) with the library package name. Here, it is imported with an asterisk.

from turtle import turtle *

In this case, you can do as you attempted in your post with no issues.

Although it will work for this particular case, generally, it is usually discouraged as there is potential for name clashing between imported names and names local to your program. For example, if you created a function with the name jump in your script and the imported library also has a native name jump, which one will your script use? There is potential of using the incorrect name in your program which may provide unexpected results.

If you want to make it less verbose, you can import turtle as an alias:

import turtle as tt

Now, for example, to make use of the method forward, you can qualify as:

tt.forward(30)

This would apply to all names imported from this library. This is the preferred way since now there is no potential for name clashing.

2 Likes

It worked, thank you!

Thanks for the explanation ^^

Also, do you know how to change the shape of the cursor to that of a turtle? because it won’t change.

To change the shape of the cursor, call the turtle.shape() function and pass it one of these strings: 'turtle', 'arrow', 'circle', 'square', 'triangle', 'classic'. For example, 'turtle.shape('circle').

Or if you ran from turtle import * instead of import turtle, you only need to run shape('circle').

It’s documented here: turtle — Turtle graphics — Python 3.13.3 documentation

You can also call turtle.hideturtle() and turtle.showturtle() to hide and show the cursor.

1 Like