0
votes

I'm getting this error from the code, "UnboundLocalError: local variable 'lowest' referenced before assignment". Why am I getting this? What does 'referenced before assignment' in this case really mean? Because I think that I assign the variable 'lowest' only after 'scores' variable is defined. Any help would be appreciated.

def main():
        scores = get_score()
        total = get_total(scores)
        lowest -= min(scores)
        average = total / (len(scores) - 1)
        print('The average, with the lowest score dropped is:', average)


def get_score():
        test_scores = []
        again = 'y'
        while again == 'y':
                    value = float(input('Enter a test score: '))
                    test_scores.append(value)
                    print('Do you want to add another score? ')
                    again = input('y = yes, anything else = no: ')
                    print()
        return test_scores

def get_total(value_list):
        total = 0.0
        for num in value_list:
                    total += num
        return total


main()
3

3 Answers

3
votes

You're using -=, which requires a starting value. But you don't provide a starting value. In context it looks like you meant to use = instead.

3
votes

That's because in main() you are saying

lowest -= min(scores)

which is essentially lowest = lowest - min(scores). Since you don't have lowest set previously, you'll get that error

1
votes

Your lowest variable is not defined. You use "lowest -= min(scores)" which means subtract min(scores) from lowest, but lowest doesn't exist yet. Based on the name of the variable I'm guessing you want to do:

def main():
    scores = get_score()
    total = get_total(scores)
    lowest = min(scores)
    average = total / (len(scores) - 1)
    print('The average, with the lowest score dropped is:', average)