In python NameError: name ‘square’ is not defined we face this error message when we set up the turtle and want to turtle shape square but we type without string or quotation mark (“ ”) so python considers square as code, not a shape then the python interpreter shows this error message, If you want to fix this error you need to just add a square with string or quotation mark (“square”) then we are able to fix this error in python, For more clarification, see the below example.
Wrong Code
import turtle
import time
import random
# Set up the screen
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("lightgreen")
wn.setup(width=600, height=600)
# Create the snake head
head = turtle.Turtle()
head.speed(0)
head.shape(square)
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"
Error Massage
Traceback (most recent call last):
File "/home/kali/python/webproject/Py_Games/tsnake/main.py", line 14, in <module>
head.shape(square)
NameError: name 'square' is not defined
Wrong code line
head.shape(square)
Correct code line
head.shape("square")
- Improper action: square without string or quotation mark.
- Correct action: square with string or quotation mark (“square”).
Entire Correct Code line
import turtle
import time
import random
# Set up the screen
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("lightgreen")
wn.setup(width=600, height=600)
# Create the snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"
What is NameError: name ‘square’ is not defined?
In python, we face this error message when we set up the turtle and want to turtle shape square but we type without string or quotation mark (“ ”) so python considers the square as code, not a shape and the python interpreter shows this error message.
How to fix NameError: name ‘square’ is not defined?
If you want to fix this error you need to just add a square with a string or quotation mark (“square”) then we are able to fix this error in python, For more clarification, see the above example.
For more information Visit YouTube Channel.
