Search code examples
pythonpython-3.xstringconcatenationtypeerror

Python TypeError: unsupported operand type(s) for +: 'int' and 'str'


I Have been working on a project and get the following error: TypeError: unsupported operand type(s) for +: 'int' and 'str'.. Any help would be greatly appreciated.If you could help shorten it as well that would be a bonus. Also if you could assign certain keys to read only certain code then that would be great too. For example if i click 1 then it will read only the #Printing The numbers in highest to lowest order and vice versa. and so on for each section. Thanks! :P

This is my code:

Code.

#Printing the Numbers in Highest to Lowest Order and vice versa.

Num1 = input("Enter a Score (Student 1)? ")
print ("The first Score is " + Num1)

Num2 = input("Enter a Score (Student 2)? ")
print ("The Second Score is " + Num2)

Num3 = input("Enter a Score (Student 3)? ")
print ("The Third Score is " + Num3)


my_list = [Num1, Num2, Num3]
print("This is the order from Lowest to Highest")

my_list.sort()

for i in range(len(my_list)):
    print(my_list[i])

print("This is the order from Highest to Lowest")

my_list.sort(reverse=True)

for i in range(len(my_list)):
    print(my_list[i])

print("\n")
print("\n")

#Printing The average of the results for each student.

Student1 = input("Enter a Score (Student 1)? ")
print ("The First Score is " + Student1)
Student2 = input("Enter a another Score (Student 1)? ")
print ("The Second Score is " + Student1)
Student3 = input("Enter a final Score (Student 1)? ")
print ("The Final Score is " + Student3)

Student4 = input("Enter a Score (Student 2)? ")
print ("The First Score is " + Student4)
Student5 = input("Enter a second Score (Student 2)? ")
print ("The Second Score is " + Student5)
Student6 = input("Enter a finalScore (Student 2)? ")
print ("The final Score is " + Student6)



print("This is the average of student 1:")
print (sum(Student1 +Student2 + Student3) / float(len(Student1)))

print("This is the average of student 2:")
print (sum(Student2) / float(len(Student2)))

print("This is the average of student 3:")
print (sum(Student3) / float(len(Student3)))

#Printing the names alphabetically.

Name1 = input("Enter a name (Student 1)? ")
print ("The first name is " + Name1)

Name2 = input("Enter a name (Student 2)? ")
print ("The Second name is " + Name2)

Name3 = input("Enter a name (Student 3)? ")
print ("The Third name is " + Name3)

mylist = [Name1, Name2, Name3]
mylist.sort()

for x in sorted(mylist):
    print (x)

Solution

  • The problem is that you are trying to concatenate String message with an int value that you introduce in your input.

    Try to print the messages casting to string the input, like this:

    print ("The First Score is " + str(Student1))