-2

I am trying to make a python script that calculates the standart deviation of numbers in a list

    for i in listofnums:
        total += (i - sumofnums) ** 2


    result = math.sqrt(total / (len(listofnums) -1))

This is the part of the program that I'm having issues with, I cannot use the total variable for getting the result for the result variable

    total += (i - sumofnums) ** 2
    NameError: name 'total' is not defined

Nearly every tutorial/documentation I looked at, it shows that you can access variables like total outside the for loop

Am I doing something stupid here?

nuudul
  • 5
  • 3

1 Answers1

-1

I believe This is exactly what you are looking for: Python 3: UnboundLocalError: local variable referenced before assignment

with that said, doing something this simple would help:

total=0 #just add it here
for i in listofnums:
        total += (i - sumofnums) ** 2


result = math.sqrt(total / (len(listofnums) -1))