Mar-26-2019, 08:40 PM
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.
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.
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.