Python UnboundLocalError on my function return. Something to do with try except?
Question:
def analyzeList(lst):
try:
#minimum
min = 0
currentmin = float(lst[0])
for i in range(0, len(lst)-1):
if currentmin > float(lst[i+1]):
currentmin = float(lst[i+1])
min = currentmin
print(min)
#maximum
max = 0
currentmax = float(lst[0])
for i in range(0, len(lst)-1):
if currentmax < float(lst[i+1]):
currentmax = float(lst[i+1])
max = currentmax
print(max)
#sum
sum = 0
for i in lst:
sum += i
average = (sum/len(lst))
except ValueError:
pass
return min, max, sum, average
This is my code. I’m trying to get min, max, sum, and average of list that has random number and letters.
Been trying for hours, not sure how to get the pass UnboundLocalError min referenced before assignment.
I tried putting the return inside the try except but that did returned nothing which is as expected but it didn’t give me any error code. Seems like the return is thinking that there is no min, max variable?
Error:
Exception has occurred: UnboundLocalError
local variable 'max' referenced before assignment
File "C:UsersdavidDesktopPractice Python Filesquestions.py", line 60, in analyzeList
return min, max, sum, average
File "C:UsersdavidDesktopPractice Python Filesquestions.py", line 70, in main
data = analyzeList(numbers)
File "C:UsersdavidDesktopPractice Python Filesquestions.py", line 73, in <module>
main()
UnboundLocalError: local variable 'max' referenced before assignment
Answers:
The problem happens when the code hits the first thing in lst
that can’t be converted to a float
. A ValueError
is raised while calculating min
and your code never gets to assigning an initial value to max
. Execution drops out of the exception handler and you try to return a max
that never had any value bound to it.
I’m going out on a limb here, but it seems like you want to get stats on the subset of the list that can be converted to a float. That means you need your exception handling inside of the loop. Its easiest to get the float conversion done early and deal with that list for sums and etc.
Python has functions for min
, max
and sum
, so no need to do this manually …. but I did anyway.
def analyzeList(lst):
floaters = []
for value in lst:
try:
floaters.append(float(value))
except ValueError:
pass
if not floaters:
return ??? no floats, what should this be?
min = max = sum = floaters[0]
for value in floaters[1:]:
if min > value:
min = value
if max < value:
max = value
sum += value
average = sum/len(floaters)
return min, max, sum, average
You could slim this down with
def analyzeList(lst):
floaters = []
for value in lst:
try:
floaters.append(float(value))
except ValueError:
pass
if not floaters:
return ??? no floats, what should this be?
return min(floaters), max(floaters), mysum:=sum(floaters), mysum/len(floaters)
def analyzeList(lst):
try:
#minimum
min = 0
currentmin = float(lst[0])
for i in range(0, len(lst)-1):
if currentmin > float(lst[i+1]):
currentmin = float(lst[i+1])
min = currentmin
print(min)
#maximum
max = 0
currentmax = float(lst[0])
for i in range(0, len(lst)-1):
if currentmax < float(lst[i+1]):
currentmax = float(lst[i+1])
max = currentmax
print(max)
#sum
sum = 0
for i in lst:
sum += i
average = (sum/len(lst))
except ValueError:
pass
return min, max, sum, average
This is my code. I’m trying to get min, max, sum, and average of list that has random number and letters.
Been trying for hours, not sure how to get the pass UnboundLocalError min referenced before assignment.
I tried putting the return inside the try except but that did returned nothing which is as expected but it didn’t give me any error code. Seems like the return is thinking that there is no min, max variable?
Error:
Exception has occurred: UnboundLocalError
local variable 'max' referenced before assignment
File "C:UsersdavidDesktopPractice Python Filesquestions.py", line 60, in analyzeList
return min, max, sum, average
File "C:UsersdavidDesktopPractice Python Filesquestions.py", line 70, in main
data = analyzeList(numbers)
File "C:UsersdavidDesktopPractice Python Filesquestions.py", line 73, in <module>
main()
UnboundLocalError: local variable 'max' referenced before assignment
The problem happens when the code hits the first thing in lst
that can’t be converted to a float
. A ValueError
is raised while calculating min
and your code never gets to assigning an initial value to max
. Execution drops out of the exception handler and you try to return a max
that never had any value bound to it.
I’m going out on a limb here, but it seems like you want to get stats on the subset of the list that can be converted to a float. That means you need your exception handling inside of the loop. Its easiest to get the float conversion done early and deal with that list for sums and etc.
Python has functions for min
, max
and sum
, so no need to do this manually …. but I did anyway.
def analyzeList(lst):
floaters = []
for value in lst:
try:
floaters.append(float(value))
except ValueError:
pass
if not floaters:
return ??? no floats, what should this be?
min = max = sum = floaters[0]
for value in floaters[1:]:
if min > value:
min = value
if max < value:
max = value
sum += value
average = sum/len(floaters)
return min, max, sum, average
You could slim this down with
def analyzeList(lst):
floaters = []
for value in lst:
try:
floaters.append(float(value))
except ValueError:
pass
if not floaters:
return ??? no floats, what should this be?
return min(floaters), max(floaters), mysum:=sum(floaters), mysum/len(floaters)