Weird error


#1

https://www.codecademy.com/en/courses/learn-python/lessons/practice-makes-perfect/exercises/digitsum

I got a very weird error. I’ve never seen this error before. Can someone please explain why does this error occurs?
This is error message i got.
Your function crashed on 434 as input because your function throws a "local variable 'total' referenced before assignment" error.

Here’s my code.

def digit_sum(n):
  if n<0:
    print "n is negative"
  elif n>0:
    if type(n) != int:
      print "n is not an interger"
    elif type(n) == int:
      total == 0
      number = str(n)
      for num in range(len(number)):
        total += num
        return total

#2

this code has several issues, but the error message you currently get is caused by this line:

total == 0

two equal signs means check if equal, so this line of code checks if total (undefined variable) equals zero.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.