-1

I am supposed to write a program in Python that asks for grades one at a time. When the user enters “done” , calculate the following: grade average.

This is what I have so far:

def main():
    user_input = input("Enter grade: ") 
    number = user_input
    while True:
        user_input = input("Enter grade: ")
        if user_input == "done":
            break
    avg(number)

def avg(a):
    average = sum(a)/len(a)
    print(average)

if __name__ == "__main__":
    main()

Whenever I enter "done," the program gives me this error.

TypeError: 'int' object is not iterable

I have tried changing the user_input variable to :

user_input = int(input("Enter grade: "))

But, another error: TypeError:

'int' object is not iterable user input

I am extremely new to programming. Can anyone help me work this out? I have been searching online for the past two hours and have not found anything that did not just produce another error.

2

2 Answers 2

1

I am noticing some things which may solve the issue for you.

  1. You're feeding number to the avg function when really you want to give it a list of numbers.
  2. I think you should do something like this: make a list called numbers and append each user input to that list. Then use the avg function on the numbers list.
Sign up to request clarification or add additional context in comments.

Comments

0

There are a few flaws in your logic.

  • Each time you ask for user input in main(), you override the value of user_input. What you should be doing, is gathering each numbers in a list().
  • What the errors Python raised are telling you, is that the builtin function sum(), takes a list of numbers, not a single number, which your passing in.
  • the input() function returns a string, so you need to convert the input to a integer.

I would rewrite your program as the following:

def main():
    # create a list to store each grade
    # that the user inputs.
    grades = []

    # while forever
    while True:
        # get input from the user.
        # I am not converting the input to a integer
        # here, because were expecting the user to
        # enter a string when done.
        i = input("Enter grade: ") 

        # if the user enters 'done' break the loop.
        if i == 'done':break 

        # add the grade the user entered to our grades list.
        # converting it to an integer.
        grades.append(int(i)) 

    # print the return value 
    # of the avg function.
    print("Grade average:", avg(grades))

def avg(grades):
    # return the average of 
    # the grades.
    # note that I'm using the builtin round()
    # function here. That is because
    # the average is sometimes a 
    # long decimal. If this does not matter 
    # to you, you can remove it.
    return round(sum(grades)/len(grades), 2)

# call the function main()
main()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.