[Tutor] Is this a scope issue?

Alan Gauld alan.gauld at yahoo.co.uk
Mon Mar 6 03:28:01 EST 2017

On 06/03/17 01:33, Rafael Skovron wrote:
> This project compares two text files with parcel numbers. I think I'm
> messing up the function call. I'm getting this error:
> Traceback (most recent call last):
>   File "amador.py", line 48, in <module>
>     remaining_parcels(auctionlist,removedlist)
> NameError: name 'auctionlist' is not defined

Your subject line is correct - it is a scope issue.

> fname = 'saclisting.txt'
> removed = 'removed.txt'
> def get_parcel_number(fname):
>     pattern = r'^(\d{3})-(\d{3})-(\d{3})-(\d{3})'
>     auctionlist = []

Here you create a local variable auctionlist.
local variables only exist within the function that creates them.

>                     return auctionlist

You return the auctionlist object from the function but....

> get_parcel_number(fname)

You never store the returned object so it gets destroyed.

> get_removed_number(removed)
> remaining_parcels(auctionlist,removedlist)

Now you try calling this function with auctionlist as an
argument but thee is no such variable (it only existed
inside the first function). You need to store the result
of the first function and use that stored value in this
function call.

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list