Ex26 brings up _jars not defined

I made it through most of ex26 and really took stock of what I have learned and I’d say, not very much. When I look at the questions others have asked up to this point I’m very discouraged because I don’t have a clue at to what they are asking. Anyway, I have a new one in my code that warns me that _jars is not defined. I looked on the net, in another Python book and even a Pure Basic book to see what “defined” has to do with it. I’m going to attach a .jpg file of my screen.


Hopefully you can make it out even if you have to load it into a viewer. There is another thing up there where it says filename and instead of putting something there, it just says filename. I may give this up as I really can’t explain what 90% of the code even does. Thank you Not2Smart

Hi Ric. Nice too see you here again.

First “defined”. Its when you attach a word to a value. Like this: jars = 10.

You have put an underscore (_) to jars “_jars”. Don’t do that. It has a special meaning i python to put underscores in front of variables (like jars). I have not learned what. So I cannot explain that part.

The error message (Traceback) gives you some clues what the problem is and where.
In your case you should have a look at line 50 and 55 in ex26.py.

My guessing is that you at first typed “jars”. Then at line 50 and 55 you started to add a underscore to jars (_jars) which python does not recognise.
Its only what I think from what I see. Please tell if I a wrong or not.

1 Like

@ulfen69 the use of an underscore in the python community and other languages is to signify privacy.

In C# for instance, if a method is private, it cannot be accesses from outside of the class. This is a useful way of controlling ‘getting/setting’ values as you can make a non-private method accessible outside the class, but hand it off to a private method internally that does some restricted stuff.

Python has no concept of privacy in the code, only in programmers behaviours. So you can also use the underscore to ‘indicate’ privacy but the code won’t be restricted to access.

I also saw the underscore used by itself in python when unpacking to variables, if a value is of no importance. For example:

aProgrammerTuple = (45, male, Graham, UK)
# unpacked but ignoring the name
age, gender, _, country = aProgrammerTuple

Sorry for the transgression@Not2Smart

1 Like

Hello @gpkesley

Thanks for explanation.
So its more of a naming convention when to use underscores?
I guess there is other ways if one want methods not to be used elsewhere than in a class.

If you name a method with two leading underscores it will be renamed internally, so it’s kind of private. Not really restricted, but still… it’s called name mangling.

2 Likes

Thanks @florian - reading that suggests it’s just a naming convention to avoid accidents of naming conflicts with sub or super classes, but they are fully accessible/editable which is interesting.

Probably why banking apps aren’t written in python :slight_smile:

Yes, that’s right. They should be written in a mixture of Haskell and Assembly anyway, just to make sure nobody understands the code. :wink:

Most of the clients I’ve worked with still use COBOL …

Hey Ric, we’d need to see your code my friend, otherwise we can’t figure out anything. Remember you can post code like this:

[code]
# your code here
[/code]

Or like this:

```
# your code here
```

Those are backticks, not single-quotes. You can also copy-paste that PowerShell output in the same way instead of posting a screenshot. That makes it so we can read it easier and reference it.

I’m guessing your problem is you have a variable names _jars with a single underscore, but then you wrote it later with __jars with two underscores. I, however, did not use any of those characters so I’m wondering why your version of the exercise has that. If you post your code I can see.

I’ll try to translate like a warehouse.
In python, when you assign a value to a variable or box aka jars.
the compiler reserves a bit of memory from your RAM/warehosue to put that value into box jars

you have a statement; jars = jelly_beans / 1000
So far so good. you just put 1 thousandth of your jelly_beans into the box jars

the error in the code is probably the following. _jars is being called upon.
with: crates = _jars / 100; this copies whatever is in box _jars divides it by 100 and puts it into a new box crates
but the compiler cannot find box _jars because you never said what’s in it.

Hence why your RAM/warehouse is really confused right now. and the logistics manager 'Mr. Tom Compiler is frowning at you. :stuck_out_tongue:

disclaimer: do not confuse box with crates in this explanation. I’m no longer responsible for what happens in your RAM warehouse. “end of line”

Zed: I have decided to put this book aside for a few months and maybe find a book that I like better. I’m disappointed and embarrassed at how little I understand. As for the things you said, like me having two underscores in front of ‘jars’, I don’t see that. Also, the reason I did a screen dump was because that way you get to see all the code and what the return was. I don’t know why you have a hard time seeing it, just click on the expanding arrow, and then put your cursor on the screen photo and click again. It then is the size of the full screen. I’m sorry I wasted so much of your time as well as others. I will say that the thing I disliked the most of your book is that you expect students to go to Google and look up the answers. Many times I didn’t even know what to look for, and other times the subject was talked about in such a complicated way, that it was of no help. Also, at the end of each chapter is usually a blank page and a half. I wish you would have explained things a little more on those blank pages. My suggestion is to rewrite the book. Thank you anyway. Ric, the Not2smart guy.

Well Ric, let me go through and point out what I wrote:

Hey Ric, we’d need to see your code my friend,

What I was saying is you need to paste the contents of the ex26.py file into here so we can look. In fact, this is something you do all the time that makes it harder for people to help you. Why won’t you show people your code? This image is not the code so, honestly, nobody can help you as much as you need without the actual code.

The next thing is, this image is actually still very grainy and hard to see no matter how large my screen is, but the most important thing that’s difficult about the image is that it’s an image. If you pasted the output from this, by doing a copy-paste of the PowerShell output, then I could actually select the characters and see if that’s a __jars or a _jars.

The words “name _jars is not defined” means that the variable you are attempting to use names _jars is not defined at that point in the code. It’s like you have this:

jars = 10
print(_jars / 2)

You defined the variable jars. It is literally and only the only name for that variable. Not _jars, not __jars or anything else. Only the exact characters j, a, r, and s. You define a variable by assigning to it, like I did here with jars = 10. This error means that you are trying to use _jars, but you never assigned with the equal sign = to _jars so it does not exist.

The fix is to find out what you actually named that variable, or to fix the name to what is already defined. Another way to look at this is you spelled it wrong.

Now, you say you can’t google for the error, but here’s what I get when I take what you highlighted and paste it into google:

https://www.google.com/search?q=python+name+is+not+defined&oq=python+name+is+not+defined

I did have a video where I google for some errors and explain that how you google for the error is you type: “python” then the error into google and it comes up with most of the answers.

But, I agree Ric. You gave it a decent try, but I think there is something going on with how you are using your computer that is making it difficult for you to ask for help. There’s something about how you never seem to be able to copy-paste anything, can’t seem to see or remember important details, and can’t seem to post your code when asking for help. I think when you do go to a different book, you should definitely fix whatever it is that’s stopping you from pasting text here. I don’t know what it is, but without pasting the text you’re working on it makes it nearly impossible to help you.

Good luck Ric. I’m sure you’ll get it if you keep pushing and trying.

1 Like

I remember a teacher in high school saying to all of the class, “you should not forget something if you actually learned it.” My problem is that I have not actually learned it. If something is not made clear, then we don’t learn things. As for not posting code, many times I didn’t think it was necessary because you wrote the book and have the code there. It was later that I discovered that some of the people don’t have the book. I have been looking over the books on Amazon and there are new books coming out all the time. Perhaps after doing a different book, I will find yours to be ‘to easy’ and will think that you should have named it ‘Learn Python The Easy Way’. My niece’s husband is the head of the computer dept. at a software place in downtown Milwaukee, Wi. and has heard good reports about your book. However, he is a brain with several degrees. Plus he has one of the smoking hottess babes in Milwaukee for a wife. Not2smart.

A free service run by Zed A. Shaw for learncodethehardway.org.