How to calculate user input in python
<p>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.</p>

<p>This is what I have so far:</p>

<pre><code>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()
</code></pre>

<p>Whenever I enter "done," the program gives me this error.</p>

<blockquote>
  <p>TypeError: 'int' object is not iterable</p>
</blockquote>

<p>I have tried changing the user_input variable to :</p>

<blockquote>
  <p>user_input = int(input("Enter grade: "))</p>
</blockquote>

<p>But, another error: TypeError: </p>

<blockquote>
  <p>'int' object is not iterable user input</p>
</blockquote>

<p>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.</p>

<p>There are a few flaws in your logic.</p>

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

<p>I would rewrite your program as the following:</p>

<pre><code>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()
</code></pre>

python input