【问题标题】:TypeError: unsupported operand type(s) for +: 'int' and 'str' errorTypeError:+ 的不支持的操作数类型:“int”和“str”错误
【发布时间】:2015-05-29 00:52:16
【问题描述】:

我的代码有问题,它说

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)))

【问题讨论】:

  • 我认为您必须将 Score 输入转换为 int,它们最初被解释为字符串
  • 你能解释一下你想让它做什么吗?另外,您能否将代码减少到最少以显示错误?
  • 我想要它,这样我就可以使用我的输入作为数字来计算总和

标签: python


【解决方案1】:

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

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

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

【讨论】:

  • 我现在遇到了这个问题 Traceback(最近一次调用最后一次):文件“\\CUR-FSM\10brierleye$\Computing CA 2\task 3 average.py”,第 57 行,在 Stu3AVG = (sum(Score3)/float(len(Score3))) #定义平均TypeError: 'int' object is not iterable
  • 该行不会出现在您发布的代码中。实际上,您可以 a) 打开一个新问题,或者 b) 尝试了解错误的作用。编程就是要理解问题,而你目前所做的只是不断地跑到 StackOverflow 来report 错误。我建议真正阅读错误。它确实说明了问题所在。
  • 我会尝试向您展示我的整个代码,但是回复太长了,基本上我输入了一个名称和一些值来创建输入分数的平均值,将名称按字母顺序排序,然后将平均值按从高到低的数字顺序排序,但它表示输入的整数不可迭代,我不知道为什么:L
【解决方案2】:

将默认输入的字符串转换为整数,以便计算平均分数:

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

【讨论】:

  • 我不断收到消息:'int' 对象不可迭代
  • 你在打分吗?发布您更新的代码以进行调试。
  • 您使用的是 Python 2 还是 Python 3?
  • Score7 = int(input("第三个人第一次在他们的测试中得到了什么?")) Score8 = int(input("第三个人第二次在他们的测试中得到了什么? ?")) Score9 = int(input("第三个人第三次考试得到了什么?"))P1S = [Score1, Score2, Score3] P2S = [Score4, Score5, Score6] P3S = [Score7, Score8, Score9] print ("here are the scores of",Name1,",well done") # 定义分数 print(P1S) print ("here is the average score of",Name1,", Well Done") # make平均分数 print(sum(P1S)/float(len(P1S)))
  • python 3.2 我相信69
【解决方案3】:

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

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)))

你能告诉我你是如何提供输入的吗?

【讨论】:

  • 我通过手动输入提供输入,我仍然得到 Int is not iterable 问题,即使我将输入定义为整数(因为我需要在稍后的等式中使用它代码)
  • 行号是 56,错误是 INT 不可迭代,我不知道为什么,因为它在我设置值时起作用,但现在我已经添加了一种输入方式它已经打破的价值观
猜你喜欢
  • 2016-04-15
  • 2012-11-29
  • 2012-12-31
  • 1970-01-01
  • 2021-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多