I’m taking a python class and ran into an issue I don’t understand. I’m a total beginner.
This loop gives me the error: “variable is not defined”
mysum = 0
for count in counts:
mysum = mysum + int(count.text)`
It’s so maddening because I know I’ve done something similar before and it’s worked.
For context, I’m trying to parse some XML with ElementTree. I’ve got all the nodes selected and I want the loop to change the string into an integer and create a total.
I’m making great headway in this class, but I still tend to make simple mistakes.
Exactly what sort of data is in counts? Have you checked that it supports a .text property? If it’s already a string you should just be able to cast it directly with int(count).
Counts is a list of elements with memory addresses. At least, that’s what it looks like if I print it to the screen. I don’t know what to properly call it. It’s a list of XML nodes from ElementTree.
If I do print count.text I get the list of numbers I want.
Glad you figured out a workaround. As for figuring out the original source of the problem, can you post the full code? Does the error message not specify which variable it’s talking about or give a line number? And double-check your formatting – not sure if it’s just from copying it onto the board, but you’ve got that line indented 8 spaces and there’s a stray apostrophe at the end.
Well, if that’s the full code, then the error message is exactly right. It’s missing the
mysum = 0
line from the original post. So the computer is trying to figure out the value of mysum + int(count.text), only to fail because mysum doesn’t have any value assigned to it.
First, I am sorry this has been so sloppy, but I’m glad we’ve figured it out. I have been recreating the code from memory, because the actual code is on my other computer. I just added that variable on this computer, and it worked, just like I thought it should have earlier today. So crazy. I am positive I had that variable assigned, but was still getting the error. I may have had some other typo in that variable.
On the up side, I figured out another way to get it done, so that’s cool. And with your great help, I figured out the probably cause of my confusion. Thanks much.
Hey @TimElhajj , next time plop your code into an online IDE like PythonFiddle and then link it, so people can run it directly. It’s a huge boon to helping get problems solved in this space!
Also, forgive me because I feel like I should know the answer to this but don’t: are you new to Python, or new to programming and Python?
Oh, I use slack at work but I haven’t really thought about it for, uh, everyday. Do I just search for qt3-leagues? I am kind of a slack newbie. I will check it out.
Wow, PythonFiddle looks pretty awesome. I have been floundering with programming for a few years now, but I’m finally starting to get my feet under me with this latest Python course. So, yes, to both, new to Python and new to programming and Python.
First I did the Python codeacadmy course (the icebreaker), then I signed up for the Coursera course Python for Everybody. It’s an excellent course and things are really coming together for me. I highly recommend.
Cool, what are you doing to get your feet under you? Have you done any programming before? I’ve had it on my goals list for a long time, but I’m just now starting to get the hang of programming in general. I write terrible, hacky, incompetent code, but still find it super satisfying to accomplish these kinds of tasks. :)
Keep in mind there’s basically a *fiddle for everything. And there are often quality alternates depending on the language in question (there’s even a variety of Database related ones that allow you to set up schemas with scripts, add data, then create the objects and run queries against said objects). This stuff is a godsend for getting other eyes on stuff. This isn’t exactly applicable here (or rather, what you provided already qualified), but when seeking help it’s good to follow the Jon Skeet practice of “short but complete programs”. That is, set up the smallest possible example that runs the code you need to run to demonstrate a problem. I as I said your example here was nicely contained but it’s a good think to keep in mind.
I think Codecademy is a quality starting resource, I was considering suggesting it although I haven’t taken the Python course. I know of Coursera but I’ve never sampled; glad to hear it was helpful. I’m curious how much did either of them get into Python’s functional aspects (e.g. the functions map, reduce).