[Tutor] Is this a scope issue?

Mats Wichmann mats at wichmann.us
Mon Mar 6 10:35:53 EST 2017


On 03/05/2017 06:33 PM, 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
> 

In this code I'd note a couple of things besides "the answer" which was
already given.

- you seem to have an indentation problem with the last line of each
function.  Not sure if that was just a paste problem; but the way the
"return" statements are placed, they would execute on the first match,
not after the whole loop. Indeed, as posted I'd expect you would get an
IndentationError because they don't line up with any block; do make sure
in your real code they're indented at a level that places them outside
the scope of the "with" statement in the first two and outside the "for"
loop in the third.

- you've got one-liner comments at the beginning of each function
definition. Might as well follow convention and turn those into
docstrings. As in for example:

def get_removed_number(removed):
    '''this grabs the removed parcel numbers and stores to a list'''

- assuming you're a beginner since you are posting here, you could keep
in mind as you learn more that many loop+condition test constructs to
populate a list can be recoded as a list comprehension. That is:

    remainingparcels =[]
    for parcel in auctionlist:
        if parcel not in removedlist:
            remainingparcels.append(parcel)

is able to be rewritten as:

    remainingparcels = [parcel for parcel in auctionlist if parcel not
in removedlist]

at that point you might question whether you even want to have a
separate remaining_parcels function, this one-liner could just go in
your main body.  That's up to you, of course!  When I first learned
"list comprehensions" I didn't find them more readable than what they
can replace; now I definitely do.



> 
> import re
> 
> fname = 'saclisting.txt'
> removed = 'removed.txt'
> 
> def get_parcel_number(fname):
> #this retrieves parcel numbers from the saclisting file and stores to
> auctionlist
> 
> 
>     pattern = r'^(\d{3})-(\d{3})-(\d{3})-(\d{3})'
>     auctionlist = []
> 
> 
>     with open(fname,'r') as file:
>         text = file.readlines()
>         for index,line in enumerate(text):
>             if re.search(pattern,line):
>                 #add line to auctionlist if the pattern is found
>                 auctionlist.append(line)
>                     return auctionlist
> 
> def get_removed_number(removed):
> #this grabs the removed parcel numbers and stores to a list
>     pattern = r'^(\d{3})-(\d{3})-(\d{3})-(\d{3})'
>     removedlist = []
> 
>     with open(removed,'r') as file:
>         text = file.readlines()
>         for index,line in enumerate(text):
>             if re.search(pattern,line):
>                 # add  line to removedlist
>                 removedlist.append(line)
>                 return removedlist
> 
> def remaining_parcels(auctionlist,removedlist):
> #compares parcels in auctionlist to removedlist and returns parcels that
> have not bee removed
>     remainingparcels =[]
>     for parcel in auctionlist:
>         if parcel not in removedlist:
>             remainingparcels.append(parcel)
>                 return remainingparcels
> 
> 
> 
> 
> get_parcel_number(fname)
> get_removed_number(removed)
> remaining_parcels(auctionlist,removedlist)
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list