Python programming newbie question

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.

Here is the solution I came up with (minus the code to get the URL). This works but the other didn’t. No idea why.

import xml.etree.ElementTree as ET

[ URL code]

counts = tree.findall('.//count')
print 'Count:', len(counts)

mylist = []

for count in counts:
    mysum = int(count.text)
    mylist.append(mysum)

print 'Sum:', sum(mylist)

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.

What was the full error you were getting? You’re weren’t presenting enough information. It feels like a typo.

It’s the line with mysum = mysum + int(count.text)

Here is the kooky error in Visual Studio:

Here is the full code:

import urllib
import xml.etree.ElementTree as ET

address = 'http://...352547.xml'

uh = urllib.urlopen(address)
print 'Retrieving', address
data = uh.read()
print 'Retrieved',len(data),'characters'
tree = ET.fromstring(data)


counts = tree.findall('.//count')
print 'Count:', len(counts)


for count in counts:
    mysum = mysum + int(count.text)

print 'Sum:', mysum

Above in your first post you have mysum = 0, but in this last snippet you’ve never declared mysum before using it.

The mysum on the right of the equal sign is undefined.

Learn to love your compiler, it usually correctly tells you what you did wrong. :)

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.

edit: bah, ninja’ed.

Thanks guys! I appreciate the help.

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.

Glad to know there are other Python folks on here. I’m suffering through the same as you, Tim, trying to pick it up for use in my job.

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?

We also do a lot of programming help and troubleshooting on the qt3-leagues Slack channel.

That’s for sure. Those guys are the best!

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.

Hi @peacedog.

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. :)

@Dave_Perkins can hook you up.

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).

What tools are you using, may I ask?

Aka a Short, Self Contained, Correct (Compilable), Example. Compilable doesn’t really apply to Python, however.

Hopefully the answer is “PyCharm”? The IntelliJ tools are great and really improve the programming experience :)