Hi,

```word = []
def letter(a):
for each in a:
if each.isalpha():
word = word+1
word.append
else:
digit = digit+1
digit.append

#letter(input("The letter with digit : "))
letter("Hello123")
print(word)```

Error:

```Traceback (most recent call last):

File "", line 13, in
letter("Hello123")

File "", line 5, in letter
word = word+1

UnboundLocalError: local variable "word" referenced before assignment```
Jan 22, 2019 in Python 1,796 views

## 2 answers to this question.

The reason for this error is that the variables word and digit are not accessible inside the function. To overcome the issue you are facing, you need to declare the variables - word, digit as global. Also, define the variables as an integer instead of list as they just store the number of characters and digits in the given string.

Try the below modified Python code and check:

```word=0

digit=0

def letter(a):

global word

global digit

for each in a:

if each.isalpha():

word=word+1

else:

digit=digit+1

#letter(input("The letter with digit : "))

letter("Hello123")

print(word)

```
• 69,230 points

Inside your function when you say word=word+1, actually the word variable after the equal sign won't be found since you are altering it already in the expression assignee. To overcome this issue there are two solutions, you either use global (which is not recommended) or first try to save the word and digit value in local variables then use them in your expression, see below sample code:

word=0
digit=0

def letter(a):
w = word
d = digit
for each in a:
if each.isalpha():
w=w+1
else:
d=d+1
return w,d
word, digit = letter("Hello123")
print(word,digit)

This could be confusing at start, perhaps you need to read about variable scopes in Python.

• 140 points

## Error while printing hello world in python.

You must be trying this command in ...READ MORE

## Python error "ValueError: size needs to be (int width, int height)" while using pygame.Surface

pygame.Surface takes in integer values for building ...READ MORE

## How to resolve the error while importing cv2 from openCV in python?

I would recommend you go with anaconda ...READ MORE

## Python error "UnboundLocalError: local variable 'x' referenced before assignment"

You get this error because when you ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

## Lowercase in Python

You can simply the built-in function in ...READ MORE

## Python argparse error "NameError: name 'file' is not defined"

The right datatype for destination in argpasrse module ...READ MORE

## Python error "NameError: name 'sr' is not defined"

NameError: name 'xx' is not defined Python knows ...READ MORE