'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 1:[1]
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))
Solution 2:[2]
The input function will give you a number if you input one:
>>> type(input())
3
<type 'int'>
>>> type(input())
3.3
<type 'float'>
You are asking someone for a score, so presumably they're entering a number of some sort. If you then do print('The score is ' + Num1), you are trying to concatenate a string 'The score is ' with the integer referenced by Num1. The + operator understands what to do with two numbers (addition) or two strings (concatenation), but has no idea what to do with a string and a number.
Solution 3:[3]
Try this based on python 3.This is for fixing error only. I dont know what your expected output
print("This is the average of student 1:")
print (sum([int(Student1) +int(Student2) + int(Student3)]) / float(len(Student1)))
print("This is the average of student 2:")
print (sum([int(Student2)]) / float(len(Student2)))
print("This is the average of student 3:")
print (sum([int(Student3)]) / float(len(Student3)))
Solution 4:[4]
Concatenation "+" with String and different types is not allowed in Pyhton.
For example:
print('This is ' + 20) # "String + Number" is not allowed
print('This is ' + True) # "String + Boolean" is not allowed
print('This is ' + []) # "String + List" is not allowed
But if you convert the different types to String with "str()", you can do the concatenation:
print('This is ' + str(20)) # This is 20
print('This is ' + str(True)) # This is True
print('This is ' + str([])) # This is []
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ricardo Burillo |
| Solution 2 | paidhima |
| Solution 3 | itzMEonTV |
| Solution 4 | Kai - Kazuya Ito |
