-2

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?

  • by the way, don't pay attention so much to what the code is doing, because he wrote another piece of code like this and experienced the same issue – jamesjsewell May 04 '18 at 20:00
  • 1
    Your understanding of scopes is flawed – bphi May 04 '18 at 20:02
  • 1
    That's normal. The environment is fine; the code doesn't mean what your friend wanted it to mean. – user2357112 May 04 '18 at 20:02
  • You need to tell Python that `largest` is a global variable. – Christian Dean May 04 '18 at 20:03
  • 1
    `largest` does not make sense as a global or an argument; it *should* be defined inside the function. – user2357112 May 04 '18 at 20:04
  • @user2357112 True. I was simply telling the OP how to immediately fix their friend's problem. – Christian Dean May 04 '18 at 20:05
  • Also, just a friendly Pro-tip: When the Python interpreter raises an error or your program has unexpected behavior, 99% of the time the problem is with _your_ code, not the interpreter's. – Christian Dean May 04 '18 at 20:08
  • @user2357112 thanks we aren't worried so much in a quick fix because we tried with other variables at the top of the file and they were available in function scope. specifically it was an array – jamesjsewell May 04 '18 at 20:13

1 Answers1

0

You have to pass largest as an argument for the function in order to make it work.

numbers = [23, 5, 12, 94, 5 , 43, 23, 59, 10, 59, 31]
largest = 0
def find_largest(x, largest): 
    for num in x:
        if num > largest:
            largest = num
    return largest
max_number = find_largest(numbers, largest)
print(max_number)
Daniele Cappuccio
  • 1,952
  • 2
  • 16
  • 31
  • thanks, we are aware, its just that we even tried using an array from outside of the function within the function and it did work. – jamesjsewell May 04 '18 at 20:14
  • @jamesjsewell It worked because you were passing the list `numbers` to the `find_largest` function... Look at the line `max_number = find_largest(numbers)`... – Harrison May 04 '18 at 20:17