0

Why this does not work? I'm used to HDLs.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

count = 0

def increment(value):
    count = count + value

count = 10
increment(20)

My expectation when running this script is count = 30

but instead, i get an error:

File "/Users/.../untitled0.py", line 7, in increment
    count = count + value

UnboundLocalError: local variable 'count' referenced before assignment
  • If you want to assign to a global from within a function, you need to declare it `global` inside that function. In your base, you need to add `global count` to the top of `increment`. This is only needed if you assign to it. Without the declaration, it creates a local variable. – Tom Karzes Nov 04 '22 at 16:35

0 Answers0