Define the class before you use it:
class Something:
def out(self):
print("it works")
s = Something()
s.out()
You need to pass self as the first argument to all instance methods.
python - How to avoid "NameError: name is not defined" when using a class above its definition - Stack Overflow
Name error : 'name' is not defined
NameError: name '' is not defined
python - NameError: name 'i' is not defined - Why does this happen? - Stack Overflow
How to solve undefined variable in python
What causes undefined variable?
Videos
Hello,
I'm new to coding and don't know what I am doing wrong here. I am using VS Code, and below is what I've inputted. Only "john" and "programmer" are highlighted in red font, while the remaining code is either blue, green, or yellow. From my understanding, those phrases are causing the syntax error. Also, it works fine when I run the same code in IDLE Shell.
name = "john"
age = 21
weight = 160.1
occupation = "programmer"
>>>print(name, age, weight, occupation)
---->Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined
Define the class before you use it:
class Something:
def out(self):
print("it works")
s = Something()
s.out()
You need to pass self as the first argument to all instance methods.
Note that sometimes you will want to use the class type name inside its own definition, for example when using Python Typing module, e.g.
class Tree:
def __init__(self, left: Tree, right: Tree):
self.left = left
self.right = right
This will also result in
NameError: name 'Tree' is not defined
That's because the class has not been defined yet at this point. The workaround is using so called Forward Reference, i.e. wrapping a class name in a string, i.e.
class Tree:
def __init__(self, left: 'Tree', right: 'Tree'):
self.left = left
self.right = right
Hi there!
I'm a programming beginner (So original! 😲) and a friend want me to code a cli based adventure game. So I opened python and realized I would probably need classes to not end up with a messy and unreadable tree-like code. Here is a program that contains an event class and a first event followed by a second event :
import climage
from colorama import just_fix_windows_console
import os
just_fix_windows_console()
terminalSize = os.get_terminal_size().columns
class Player:
def __init__(self, name):
self.name = name
satiety = 10
health = 10
relationships = {}
inventory = []
class Event:
def __init__(self,
pointsTo,
text="",
image=False,
isImageUnicode=False,
imageWidth=terminalSize-1,
function=False,
*args):
self.text = text
self.image = image
self.isImageUnicode = isImageUnicode
self.imageWidth = imageWidth
self.pointsTo = pointsTo
self.function = function
self.args = args
def play(self):
if self.function != False:
self.function(*self.args)
if self.image != False:
displaySize = self.imageWidth
if displaySize > terminalSize-1:
displaySize = terminalSize-1
outputImage = climage.convert(self.image, is_unicode=self.isImageUnicode, width=displaySize)
print(outputImage)
print(self.text)
wait = input("Press return to continue.")
class End:
def play(self):
print("Fini.")
quit()
end = End()
event1 = Event(
pointsTo=event2,
text="This is event 1",
image="images\\person.jpg",
isImageUnicode=False
)
event2 = Event(
pointsTo=end,
text="This is event 2",
image="images\\city.jpg",
isImageUnicode=False
)
event1.play()The problem is that when I define event1, event2 do not exist at all. To avoid this error, I would need to write my story backwards, it would be so painful and ugly. So do a clean solution even exists ?
Thanks for reading !