-1

Here's my code:

window=turtle.Screen()
window.title("Pong")
window.bgcolor("black")
window.setup(width=800,height=600)
window.tracer(0)

I get this error when I run the program:

 File "C:\Users\Aditya\PycharmProjects\pythonProject\main.py", line 6, in <module>
    window=turtle.Screen()
NameError: name 'turtle' is not defined

What's the problem and how can I fix it?

ggorlen
  • 44,755
  • 7
  • 76
  • 106

3 Answers3

1

I think you forgot to import turtle

import turtle
window=turtle.Screen()
window.title("Pong")
window.bgcolor("black")
window.setup(width=800,height=600)
window.tracer(0)
redystum
  • 352
  • 3
  • 10
0

You need to import the module before calling the sub-functions within it. In your case, you need the turtle module.

import turtle
window=turtle.Screen()
window.title("Pong")
window.bgcolor("black")
window.setup(width=800,height=600)
window.tracer(0)

However, if you added the line: import turtle, and encounter the error below:

File "/usr/lib/python3.8/turtle.py", line 107, in <module>
import tkinter as TK 
ModuleNotFoundError: No module named 'tkinter'

You may refer to this post: here

Blaco
  • 109
  • 8
-2

Have you imported the Turtle module? if you didn't, try this one:

from turtle import *

Then the rest of your code

  • 1
    This would not make the OP's code work as it imports everything in `turtle` into the current namespace. – Chris Jan 29 '22 at 19:32
  • Even if this did work, it's poor practice to pollute the `globals()` namespace with 100+ turtle functions, usually leading to large amounts of confusion and bugs. – ggorlen Feb 16 '23 at 20:42