Ask coding questions

← Back to all posts
string variable nondefined?
DSAyonatan20 (0)

I have a string variable which is used as a parameter in an event via pygame and I want to say that its defined later but I don't know how so it just shows and error when I try to run it. so s there some type command I can use to make it wait until it attempts to define it. I have a few if statements for defining it and I pressing keys changes the value.

Comments
hotnewtop
RYANTADIPARTHI (6010)

try naming the variable first. To some default value like.

variable = None or variable = ""

[deleted]

You can declare the variable with no value with

variable = None
and then set it to a value later with

variable = "yourvariablehere”
click the checkmark next to my name if this helped!

LukeWright (114)

You can declare the variable with no value with

variable = None

and then set it to a value later with

variable = "yeet"

click the checkmark next to my name if this helped!

Wumi4 (486)

You can use an empty string or something to define that variable temporarily.

# The 'number' will be defined later
number = ""

# There we go!
number = "7"