Python TypeError: unsupported operand type(s) for +: 'int' and 'str'
Python TypeError: unsupported operand type(s) for +: 'int' and 'str'
我一直在处理一个项目并收到以下错误:TypeError: unsupported operand type(s) for +: 'int' and 'str'.
。任何帮助都会非常有用 appreciated.If 你也可以帮助缩短它,这将是一个奖励。另外,如果您可以分配某些键来只读取某些代码,那也很棒。例如,如果我单击 1,那么它将只读取从最高到最低顺序的#Printing The 数字,反之亦然。每个部分依此类推。谢谢! :P
这是我的代码:
代码。
#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)
input
函数会给你一个数字,如果你输入一个:
>>> type(input())
3
<type 'int'>
>>> type(input())
3.3
<type 'float'>
您要向某人索取分数,因此大概他们正在输入某种数字。如果您随后执行 print('The score is ' + Num1)
,则您试图将字符串 'The score is '
与 Num1
引用的整数连接起来。 +
运算符了解如何处理两个数字(加法)或两个字符串(连接),但不知道如何处理字符串和数字。
问题是您试图将字符串消息与您在输入中引入的 int 值连接起来。
尝试打印转换为字符串输入的消息,如下所示:
打印 ("The First Score is " + str(Student1))
根据python 3
试试这个。这只是为了修复错误。我不知道你期望的输出是什么
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)))
在 Pyhton.
例如:
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
但是如果使用 "str()" 将不同类型转换为 String,则可以进行连接:
print('This is ' + str(20)) # This is 20
print('This is ' + str(True)) # This is True
print('This is ' + str([])) # This is []
我一直在处理一个项目并收到以下错误:TypeError: unsupported operand type(s) for +: 'int' and 'str'.
。任何帮助都会非常有用 appreciated.If 你也可以帮助缩短它,这将是一个奖励。另外,如果您可以分配某些键来只读取某些代码,那也很棒。例如,如果我单击 1,那么它将只读取从最高到最低顺序的#Printing The 数字,反之亦然。每个部分依此类推。谢谢! :P
这是我的代码:
代码。
#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)
input
函数会给你一个数字,如果你输入一个:
>>> type(input())
3
<type 'int'>
>>> type(input())
3.3
<type 'float'>
您要向某人索取分数,因此大概他们正在输入某种数字。如果您随后执行 print('The score is ' + Num1)
,则您试图将字符串 'The score is '
与 Num1
引用的整数连接起来。 +
运算符了解如何处理两个数字(加法)或两个字符串(连接),但不知道如何处理字符串和数字。
问题是您试图将字符串消息与您在输入中引入的 int 值连接起来。
尝试打印转换为字符串输入的消息,如下所示:
打印 ("The First Score is " + str(Student1))
根据python 3
试试这个。这只是为了修复错误。我不知道你期望的输出是什么
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)))
例如:
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
但是如果使用 "str()" 将不同类型转换为 String,则可以进行连接:
print('This is ' + str(20)) # This is 20
print('This is ' + str(True)) # This is True
print('This is ' + str([])) # This is []