1
print("\n\nThis is a basic program to find the letter grade for a student.")
student = input("What is the students name? : ")
test1 = input("What is the first test grade " + student.capitalize() + " recieved? : ")
test2 = input("What is the second test grade " + student.capitalize() + " recieved? : ")
test3 = input("What is the third test grade " + student.capitalize() + " recieved? : ")
averageTest = (int(test1) + int(test2) + int(test3))
print(student.capitalize() + " recieved an average test grade of " + ((averageTest) / 3))

I am writing a basic grade calculator program and cannot solve this problem

TypeError: cannot concatenate 'str' and 'float' objects

I have so much more to write and im stuck on this line print(student.capitalize() + " recieved an average test grade of " + ((averageTest) / 3))

Matthew Ciaramitaro
  • 1,184
  • 1
  • 13
  • 27
Ian Thomas
  • 19
  • 1
  • 3
  • 2
    consider looking into [`str.format()`](https://docs.python.org/2/library/string.html#format-examples) – rob Oct 04 '17 at 15:55

1 Answers1

4

You can't add a string and a number (float). Instead, convert your float to a string before you add it with another string. You can do this with

str(x)

In your case, this would be:

# converting the float to a string
print(student.capitalize() + " recieved an average test grade of " + str((averageTest) / 3))

# or, to avoid using addition or conversion at all in a console print
print student.capitalise(), "recieved an average test grade of", averageTest/3

# or even using string formatting (example is py2.7)
print '%s recieved an average test grade of %s' % (student.capitalise(), averageTest/3)
MattWBP
  • 376
  • 1
  • 10
  • Thank you !! s there anything else I should do? It is a project where you have the teacher choose the weight of each research paper/class participation/final exam. It has to be user friendly... Any suggestions?? Thank you – Ian Thomas Oct 04 '17 at 15:54
  • Aside from I would recommend creating functions for things like the 'Average Test' that returns the average instead of having to divide it later. This would give you more flexibility when it comes to adding in extra parameters such as weighting for each test and allows you to code in a slightly more organised manner as it encourages thinking in 'blocks' based on things you want your code to do. For example - define tests and weighting, ask for inputs (name and test results), work out weighted results, work out letter grade from weighted results. – MattWBP Oct 04 '17 at 16:00
  • Thank you!! I really appreciate it, it has come a long way since yesterday... – Ian Thomas Oct 05 '17 at 13:06
  • No problem! Don't forget to mark the answer as accepted, and good luck finishing your project! – MattWBP Oct 05 '17 at 13:45
  • How do I make it accepted? – Ian Thomas Oct 05 '17 at 14:57
  • https://stackoverflow.com/help/someone-answers Might not be able to as the question is marked as potential duplicate, so don't worry! Back to work! – MattWBP Oct 05 '17 at 15:11