Python Forum
Unbound local error while attempting to reference a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unbound local error while attempting to reference a list
#1
I have a basic program in which I enter a business' sales, and it can use one of a few functions like finding the top selling store location and similar. Since I am in an intro class, one of these functions is a bubble sort to sort from best selling location to worst selling location, and vice versa.

def sort_hilo():
    n = len(sales)
    e = len(location)
    swapped = True
    while swapped:
        swapped = False
        for i in range(n - 1):
            if sales[i] > sales[i + 1]:
                temp = sales[i]
                sales[i] = sales[i+1]
                sales[i+1] = temp
                swapped = True
                
        for i in range(n - 1):
            if location[i] > location[i + 1]:
                temp = location[i]
                location[i] = location[i+1]
                location[i+1] = temp
                swapped = True
                
        n -= 1
        e-=1
    for i in range(len(sales)):
        location=i+1
        print('Location', location, '$', ('%0.2f'%sales))
both "sales" and "location" in the first few lines are supposed to reference lists that I globally defined in another part of the program.

The problem is, it keeps giving me back the local variable referenced before assignment for the locations list.
"e = len(location)
UnboundLocalError: local variable 'location' referenced before assignment"

But it doesn't give me this error for the sales list! I have no idea how to get it to realize that it is supposed to reference a predefined list instead of a local variable.
Reply


Messages In This Thread
Unbound local error while attempting to reference a list - by molassascookieman - Mar-26-2019, 08:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I am attempting to make a car booking system for my assignment aboo 5 12,648 Aug-08-2023, 12:50 PM
Last Post: loganmary689

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020