TypeError:+:'int'和'str'错误的不支持的操作数类型

时间:2015-03-25 11:18:05

标签: python

我的代码出现问题并且说

  

TypeError:+:' int'不支持的操作数类型和' str'

我不知道为什么。我的代码区域导致了这一点,如下所示,所有帮助将不胜感激:D!*编辑错误似乎是因为输入选项,如果这有任何帮助。

Score1 = str(input("what did the first person get in their test the first     time?"))
Score2 = str(input("what did the first person get in their test the second  time?"))
Score3 = str(input("what did the first person get in their test the third time?"))

Score4 = str(input("what did the second person get in their test the first time?"))
Score5 = str(input("what did the second person get in their test the second time?"))
Score6 = str(input("what did the second person get in their test the third time?"))


Score7 = str(input("what did the third person get in their test the first time?"))
Score8 = str(input("what did the third person get in their test the second time?"))
Score9 = str(input("what did the third person get in their test the third time?"))


P1S = [Score1, Score2, Score3]
P2S = [Score4, Score5, Score6]
P3S = [Score7, Score8, Score9]



print ("here are the scores of",Name1,",well done") # defines scores
print(P1S)
print ("here is the average score of",Name1,",Well Done") # makes average of  scores
print(sum(P1S)/float(len(P1S)))

print ("here are the scores of",Name2,",well done") # defines scores
print(P2S)
print ("here is the average score of",Name2,",Well Done") # makes average of scores
print(sum(P2S)/float(len(P2S)))

print ("here are the scores of",Name3,",well done") # defines scores
print(P3S)
print ("here is the average score of",Name3,",Well Done") # makes average of scores
print(sum(P3S)/float(len(P3S)))

3 个答案:

答案 0 :(得分:1)

明确注意用户输入的内容是字符串:

Score1 = str(input("what did the first person get in their test the first     time?"))

如果您将str()替换为int()float()(取决于您对输入的期望),您的问题应该消失,因为您会得到一个数字类型而不是而不是str

答案 1 :(得分:0)

默认情况下将字符串输入转换为整数,以便您可以计算平均分数:

Score1 = int(input("what did the first person get in their test the first time?"))

答案 2 :(得分:0)

您还可以在计算分数时使用地图。

print ("here are the scores of",Name1,",well done") # defines scores
print(P1S)
print ("here is the average score of",Name1,",Well Done") # makes average of  scores
print(sum(map(int,P1S))/float(len(P1S)))

print ("here are the scores of",Name2,",well done") # defines scores
print(P2S)
print ("here is the average score of",Name2,",Well Done") # makes average of scores
print(sum(map(int,P2S))/float(len(P2S)))

print ("here are the scores of",Name3,",well done") # defines scores
print(P3S)
print ("here is the average score of",Name3,",Well Done") # makes average of scores
print(sum(map(int,P3S))/float(len(P3S)))

还可以告诉你如何提供输入。