Python UnboundLocalError error while executing script

0 votes

Hi,

Below is the code i am trying but is showing error, please help with this code.

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 in Python by Vishal
15 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

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)

answered Jan 22 by Omkar
• 65,810 points

Related Questions In Python

+2 votes
2 answers

Error while printing hello world in python.

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

answered Mar 31, 2018 in Python by GandalfDwhite
• 1,320 points
30 views
0 votes
2 answers

What do you mean by python scripting? What is a script and a module in python?

A scripting language is a programming language ...READ MORE

answered Mar 14 in Python by rajesh kumar
1,487 views
0 votes
1 answer

Do-while loop in Python

Python does not have a do-while construct.  If ...READ MORE

answered May 11, 2018 in Python by Hamartia's Mask
• 1,580 points
59 views
0 votes
2 answers

Indentation Error in Python

Use tabs instead of spaces. This is ...READ MORE

answered Feb 14 in Python by Shashank
• 1,350 points
45 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 920 points
18 views
+4 votes
6 answers
0 votes
1 answer

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

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

answered Nov 28, 2018 in Python by Omkar
• 65,810 points
630 views
0 votes
1 answer

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

I guess you are using speech_recognition module and missing ...READ MORE

answered Nov 28, 2018 in Python by Omkar
• 65,810 points
1,663 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.