Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

HARMANDEEP PANNU
HARMANDEEP PANNU
2,077 Points

My code not working

def main():
    scores = input("Enter five test scores: ")
    scores = [int(x) for x in scores.split(",")]
    showLetters(scores)
    calc_average(scores)



def showLetters(grades):
    for x in grades:
        scores = [int(x)]
        if x >= 90 and x <= 100:
            grades = "A"
        elif x >= 80 and x <= 89:
            grades = "B"
        elif x >= 70 and x <= 79:
            grades = "C"
        elif x >= 60 and x <= 69:
            grades = "D"
        elif x < 60:
            grades = "F"
        print("{:.1f} is {}".format(x, grades))

def calc_average(grades):
    total = 0
    for el in grades:
        scores = [int(el)]
        total += el
        average = total / 5
        letter = showLetters(average)
        print("The average is: {:.1f} which is {}".format(average, letter))


main()
line 12, in showLetters
Β Β Β Β for x in grades:
TypeError: 'float' object is not iterable

1 Answer

Steven Parker
Steven Parker
231,271 Points

The wrong argument type is being passed to showLetters().

The function showLetters() takes an iterable argument (such as a list) and prints messages but does not return any value.

But in calc_average() it is being called on this line:

        letter = showLetters(average)

The variable average contains a float value at this point, which causes the error. But also notice that letter is being assigned here and will always be None, since showLetters() does not return anything.

HARMANDEEP PANNU
HARMANDEEP PANNU
2,077 Points

when I try

letter = showLetters(average)

It does not print?

Steven Parker
Steven Parker
231,271 Points

No, it won't print, instead it causes the error because average is a float value, but the function requires an argument which is iterable (like a list).

It looks like what you want here is a different function, one that would take a single value and return a letter.