vubbz
vubbz

Reputation: 21

Why is my "total" variable not defined?

Im still a very new beginner when it comes to python and my code is giving me an error when trying to run it. It is telling me that total is not defined.

def main():
    total = 0
    avg = 0
    abc_grade = 0

def calc_average(total):
    return total / 5

def letterGrade(grade):
    if 90 <= grade <= 100:
        return "A"
    elif 80 <= grade <= 89:
        return "B"
    elif 70 <= grade <= 79:
        return "C"
    elif 60 <= grade <= 69:
        return "D"
    else:

        return "F"

while(True):
    grade = int(input("Enter a Grade: "))
    total += grade
    avg = calc_average(total)
    abc_grade = letterGrade(grade)

    print("Average: " +str(avg))
    print("Grades: " +str(abc_grade))

main()

I thought that by giving "total = 0" under main would define it. Again, Im a total beginner so any help/explanation would be great.

Upvotes: 1

Views: 564

Answers (5)

Sede
Sede

Reputation: 61263

Actually there are a lot of things wrong with your code.

That being said I'll suggest that you move your while loop into your main() function and use one of the method in the above link to break out of your while loop when your done. Something like this:

def main():
    # variable declaration
    total = 0
    avg = 0
    abc_grade = 0

    while(True):
        # do something 
        ....
        # break out when done

Last but not least you should use string + str(expression) to concatenate your string in the print function there are much more better way to do this. I suggest you use format like this:

print("Average: {avg}".format(avg=avg)

Upvotes: 0

Vasanth
Vasanth

Reputation: 1278

Your while loop is called first.

the statement total += grade is where the error occurs since total is not initialized yet. Add total = 0 before the while loop.

total = 0  #Defining total
while(True):
    grade = int(input("Enter a Grade: "))
    total += grade
    avg = calc_average(total)
    abc_grade = letterGrade(grade)

On a side note, notice that the while-loop executes infinitely and main() doesn't get executed.

Upvotes: 0

Lost
Lost

Reputation: 1008

So your program flow goes like this as currently written:

While True, add some stuff do some averaging and print the results. THEN run the main method.

Indention is a big deal in python, so by having your while loop at the same indention as Main() you run all of that first. So the first time your program sees total is when you say total += grade

I suspect that you meant for all of that while logic to be part of your main method and look something like this:

def main():
    total = 0
    avg = 0
    abc_grade = 0
    input = 5
    while(input > 0):
        grade = int(input("Enter a Grade: "))
        total += grade
        input -= 1

    avg = calc_average(total)
    abc_grade = letterGrade(grade)
    print("Average: " +str(avg))
    print("Grades: " +str(abc_grade))

def calc_average(total):
    return total / 5

def letterGrade(grade):
    if 90 <= grade <= 100:
        return "A"
    elif 80 <= grade <= 89:
        return "B"
    elif 70 <= grade <= 79:
        return "C"
    elif 60 <= grade <= 69:
        return "D"
    else:
        return "F"

main()

You also need to add some logic to END your while loop. Currently I don't see anything that sets it to false. Since your average method is hard coded to only average 5 inputs, I reworked the while loop to take 5 inputs

Upvotes: 0

Chris
Chris

Reputation: 16172

The variable define in the main() function will only be available inside the main function. You can move the code that references total inside the main function to resolve the issue.

 def main():
        total = 0
        avg = 0
        abc_grade = 0


    grade = int(input("Enter a Grade: "))
    total += grade
    avg = calc_average(total)
    abc_grade = letterGrade(grade)

    print("Average: " + str(avg))
    print("Grades: " + str(abc_grade))

def calc_average(total):
    return total / 5

def letterGrade(grade):
    if 90 <= grade <= 100:
        return "A"
    elif 80 <= grade <= 89:
        return "B"
    elif 70 <= grade <= 79:
        return "C"
    elif 60 <= grade <= 69:
        return "D"
    else:

        return "F"

while(True):
    main()

Upvotes: 0

hilberts_drinking_problem
hilberts_drinking_problem

Reputation: 11602

It isn't defined in the outer scope, where your while loop is. You can either wrap your while loop in a function and call it from main, or replace main with this:

total = 0
avg = 0
abc_grade = 0

i.e. define the variables in the global scope.

Upvotes: 2

Related Questions