my friend is getting an error that should not be happening. He added a variable to the top of his file, and can't access the variable from a function in his script without getting an error.
this is his code:
numbers = [23, 5, 12, 94, 5 , 43, 23, 59, 10, 59, 31]
largest = 0
def find_largest(x):
for num in x:
if num > largest:
largest = num
return largest
max_number = find_largest(numbers)
print(max_number)
this is the error
error message: Traceback (most recent call last):
File "largestElement.py", line 10, in <module>
max_number = find_largest(numbers)
File "largestElement.py", line 6, in find_largest
if num > largest:
UnboundLocalError: local variable 'largest' referenced before assignment
why is this happening? when we move the variable definition to the function scope it works, but when we put the variable definition in the top of the script, he gets a called before reference error. Also, the numbers array doesn't have an issue and we are using it in the same way.
Could it maybe be something wrong with his conda environment?