Advertisement

Help with python :(

Started by April 24, 2016 05:37 AM
9 comments, last by Tutorial Doctor 8 years, 7 months ago

John_Smith = {name: "John Smith",}

error:


name 'name' is not defined

It seemed as if the nether grasped onto my flimsy body and swing me all around a couple of times before setting me down only to step on me, splattering my brain in the process.

A key in an associative array need to be defined. In your case the key is given by a variable which itself is not defined.

a) The key defined by a string literal:


John_Smith = { "name": "John Smith" }

print(John_Smith["name"])
b) The key defined by a string variable:

name = "name"
John_Smith = { name: "John Smith" }

print(John_Smith["name"])
print(John_Smith[name])
Advertisement

Excuse me for my newbish understanding of python (and programming in general) but what is a string literal and an associative array? I'm still quite new to all of this.

Excuse me for my newbish understanding of python (and programming in general) but what is a string literal and an associative array? I'm still quite new to all of this.


A literal is a notation for representing a fixed value throughout the source code.
eg. name = "name", here "name" is a string literal because its value will be constant throughout the source code.

An associative array is an array which associates a key with a value. Its like a dictionary! When you look for a word in a dictionary (the word is the key), you get its meaning (the meaning is its value).

Let's say you associated the variable 'name' (which was not defined) to "John Smith" in the array.
Then, when you look for the value of the key 'name', its like searching for a the meaning of an undefined word in a dictionary (which means you don't know which word you are searching for). Hence 'name' needs to be defined first or you can use a literal instead.

I'm still quite new to all of this

I suggest you to stop game programming and first make concepts of general programming clear, do many projects and then move to game programming.

If you are interested in python you might find these resources useful (free online ebooks):

Otherwise you can get this book Head First Programming


John_Smith = {name: "John Smith",}

error:


name 'name' is not defined

It seemed as if the nether grasped onto my flimsy body and swing me all around a couple of times before setting me down only to step on me, splattering my brain in the process.

Nah, computers just don't like it if they don't understand what you're saying.

They get grumpy and mumble an error, hoping you understand it and fix whatever is wrong, to make them happy again :)

To understand what is happening, let's take a step back, and use numbers instead of strings, since they are less confusing.

34

As you (hopefully) know, 34 is a number. It is one of the integer values that we have. We call such a thing a literal, since you literally write the value. Compare with 1+6+8+1+4+6+3+2+1+2. This also has the value 34, but it is not the literal value 34, you have to perform computations before you can conclude it's also 34.

With strings, the rule of a literal value is that you type text, and add either a pair or ' quotes or a pair of " quotes around it.

So in your line, "John Smith" is a string literal (it has a pair of " quotes around it), and name is not a literal (neither ' nor " pair of quotes there).

The error is however caused by something different, namely variables. Let's first do this with numbers:

i = 1

What is the value of i + 2 ?

I hope you agree it's 3. Just above it, I wrote that i equals 1, so you can replace the "i" in i + 2 by "1", which gives you 1 + 2, which is 3 after an addition.

The "i" here is a variable, a name that represents a value (when I say i I mean the value 1).

Now this one

What is the value a + 5 ?

The answer is "I don't know, since you didn't give me the value of a!"

In other words, we cannot compute anything if we don't have all the information.

Now back to your line:

{name: "John Smith",}

What is name here?

Above we concluded it is not a string literal (it has no ' or " quotes around it).

Therefore, it must be a variable. However, your code has no value assigned to the variable name !

The computer is just as confused about "name", as you were about my "a", gets grumpy, and it spits out an error:


NameError: name 'name' is not defined

The 'name' thing refers to your name in the line of code, and it says it's not defined, which is correct, as you didn't make a variable called name , eg by doing

name = "the name is"

{name: "John Smith",}

The first name in the error line is somewhat unfortunate, but it indicates the computer doesn't know at all what you mean by name in the line of code.

It cannot say 'variable', as it has no idea what name is, or what it is supposed to be.

Adding the ' name = "the name is" ' line is one way of solving the problem, but there are many more. In this case, I think you meant to have a string literal with the text name which you write as

"name"

(the pair of quotes indicates it's a string literal).

That leads to

{"name": "John Smith",}

Last but not least, there is no difference between ' quotes and " quotes. "name" and 'name' are the same thing. Use what you prefer, or like me, use whatever mood you're in :)

PS About the associative array, the {} thing is a dictionary (dict in pythoneese), and it's official name is an associative array, just like 34 has the official name literal.

Don't worry too much about those things for now, have fun programming things. When you have more experience and do more study, they will come back to you.

Oh, I didn't start doing game programming without learning programming first, it's just that I thought that the key should've been a variable instead of a string and then I got an error.

Anyways, I think I understand it now so thanks!

EDIT: Do you guys think this book is good? http://www.amazon.com/Python-Crash-Course-Hands--Project-Based-ebook/dp/B018UXJ9RI/ref=sr_1_8?s=digital-text&ie=UTF8&qid=1461517860&sr=1-8&keywords=python+programming

Advertisement

The Python site has several pages devoted to new users of the language http://wiki.python.org/moin/BeginnersGuide

It also has a book list, and your book seems to be there http://wiki.python.org/moin/IntroductoryBooks

To clarify further for someone who might view this thread later:


John_Smith = {name: "John Smith",}
error:
name 'name' is not defined

The syntax/grammar rules for creating a dictionary in python is:


variable_name = {"key":"value"}

The name of the key has to be in quotes.

The reason the error says that name is not defined is because without quotes it is treated as a variable. No variable named "name" has been defined above this code.

I have a few recommend readings:

https://gist.github.com/TutorialDoctor/980911848cad7217cfc0

https://github.com/TutorialDoctor/Software_Development

They call me the Tutorial Doctor.

Alright, thx.

The syntax/grammar rules for creating a dictionary in python is:

variable_name = {"key":"value"}
The name of the key has to be in quotes.

Not at all, Python dictionaries can have any immutable type as key, not just strings. The OP probably just used strings, as those are common in tutorials

This topic is closed to new replies.

Advertisement