1
name1 = input("Please enter in your first name :")
name2 = input("Please enter in your last name :")
prompt = "Please enter in your grades. When you are finished, enter 'quit': "
grades = []
grades2 = (input(prompt).strip())
length = len(grades)

while (grades2 != "quit"):
    grades.append(grades2)
    grades2 = input(prompt).strip()

if (grades2 == "quit"):
    average = float( sum(grades) / len(grades) )
    print (name1, name2, average)    

So I'm trying to make the program print me an average and I get this error:

    Traceback (most recent call last):
   File "C:/Users/Owner/AppData/Local/Programs/Python/Python35/Project.py",      line      13, in <module>
          average = float( sum(grades) / len(grades) )
      TypeError: unsupported operand type(s) for +: 'int' and 'str'

Help!

Gatwa
  • 47
  • 1
  • 3

3 Answers3

1

You can solve your problem by modyfing the while loop, and converting user inputs to floats, after checking if its not a quit string:

name1 = input("Please enter in your first name: ")
name2 = input("Please enter in your last name: ")
prompt = "Please enter in your grades. When you are finished, enter 'quit': "

grades = []

while True:
    an_input = input(prompt).strip()
    if an_input == 'quit': 
         break        
    grades.append(float(an_input))

average = sum(grades) / len(grades)
print(name1, name2, average)

Note. If your users provide other string that 'quit', the program will fail. So you may want to extend to this, to make sure that user input is number, before appending it to grades list.

Marcin
  • 215,873
  • 14
  • 235
  • 294
0

Your problem is that your appending strings to your list, instead of numbers. You need to cast your user input to an integer before appending them to your list. You can see this by printing your list on each iteration of your while loop:

Please enter in your first name :dummy
Please enter in your last name :name
Please enter in your grades. When you are finished, enter 'quit': 1
[]
Please enter in your grades. When you are finished, enter 'quit': 2
['1']
Please enter in your grades. When you are finished, enter 'quit': 3
['1', '2']
Please enter in your grades. When you are finished, enter 'quit': 4
['1', '2', '3']
Please enter in your grades. When you are finished, enter 'quit': 5
['1', '2', '3', '4']
Please enter in your grades. When you are finished, enter 'quit': 6
['1', '2', '3', '4', '5']
Please enter in your grades. When you are finished, enter 'quit': 

Also, you should check your user input to see if it equals the string "quit" before appending it to your grades list so that it is not appended to the list:

name1 = input("Please enter in your first name :")
name2 = input("Please enter in your last name :")
prompt = "Please enter in your grades. When you are finished, enter 'quit': "
grades = []

while True:
    grade = input("Enter a grade: ")
    if grade == "quit":
        average = sum(grades) / len(grades)
        print(name1, name2, average)
        break
    grades.append(int(grade))

Changes:

  • I used a while-true instead of a while condition because there is no need to test your user input in your while loop as well as your if statement.
  • I added the check to see if the user quits before you append the user input to your grades list.
  • I cast the user input to an integer before appending it to your grades list.
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
0

Change the second last line to

average = float(sum([float(i) for i in grades]) / len(grades) )
hvwaldow
  • 1,296
  • 1
  • 11
  • 13