0

I do not know why I am getting this error

NameError: name 'len_num' is not defined

def is_armstrong_number(num):
#Convert number to string
    num_str = str(num)
#Get length of number
    len_num = len(num_str)
#Select each number in string and store it in list
for x in range(1,len_num):
    num_list.insert(x,num_str[x])
#Convert back to int
    int(num_list)
#Compare armstrong number and list
for x in range(1,len_num):
   if num == num_list(x) ** len_num:
    print("It is an armstrong number")
else: print("It is not an armstrong number")
pass  

len_num is local to the function so I do not see the issue with the name since it is defined before it is used.

harrshaw
  • 11
  • 1
  • 3
    Your variable is only defined inside the function. The use in the for loop is outside of the function. – 9769953 Jan 25 '23 at 16:02
  • 1
    Please see the following posts that explain the scope that variables exist https://stackoverflow.com/questions/2829528/whats-the-scope-of-a-variable-initialized-in-an-if-statement and https://stackoverflow.com/questions/370357/unboundlocalerror-on-local-variable-when-reassigned-after-first-use – Cory Kramer Jan 25 '23 at 16:03
  • 1
    *"len_num is local to the function so I do not see the issue"* - that everything after it is ***outside*** of the function... – Tomerikoo Jan 25 '23 at 16:05
  • You have a problem with an indentation. For loops are outside of the function. That's why you are getting NameError. Also `num_list` has not been defined before `num_list.insert(x,num_str[x])`. And the last, `int(num_list)` is wrong. You can't convert a list to int. – anymous Jan 25 '23 at 16:07
  • Check you are doing all the necessary assignations. `int(num_list)` is suppose to calculate an integer that would be immediately lost. Also, how do you expect python to convert a list into an integer? `num_list` is never defined. – Ignatius Reilly Jan 25 '23 at 16:43
  • Lists in python are indexed from 0, are you sure you want all the for loops to iterate in the range `(1, len_num)`? – Ignatius Reilly Jan 26 '23 at 16:08
  • What's the purpose of `pass`? – Ignatius Reilly Jan 26 '23 at 16:09
  • The way you convert `num` to a list of digits is overly complicated, and wrong (you miss the first digit because is out of the range of the for loop). [How to split an integer into a list of digits?](https://stackoverflow.com/questions/1906717/how-to-split-an-integer-into-a-list-of-digits) – Ignatius Reilly Jan 26 '23 at 16:13

1 Answers1

-1

Your issues could be with identation. Compare with my code:

def is_armstrong_number(num):
    #Convert number to string
    num_str = str(num)
    #Get length of number
    len_num = len(num_str)
    #Select each number in string and store it in list
    for x in range(1,len_num):
        num_list.insert(x,num_str[x])
    #Convert back to int
        int(num_list)
    #Compare armstrong number and list
    for x in range(1,len_num):
       if num == num_list(x) ** len_num:
        print("It is an armstrong number")
    else: print("It is not an armstrong number")
    pass

is_armstrong_number(2)
# Output
# It is not an armstrong number
~                      

I don't get the error after running.

Stanley Ulili
  • 702
  • 5
  • 7