2015-03-25 102 views
1

林和我的代碼有問題,它說類型錯誤:對於不支持+操作數類型(S):「詮釋」和「海峽」錯誤

TypeError: unsupported operand type(s) for +: 'int' and '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))) 
+0

我認爲你必須將'Score'輸入轉換爲int,它們最初被解釋爲字符串 – EdChum 2015-03-25 11:19:33

+0

你能解釋一下你想要它做什麼嗎?另外,您是否可以減少代碼以最小化顯示錯誤? – 2015-03-25 11:20:01

+0

我想要它,所以我可以使用我的輸入作爲數字工作到一個總和 – 2015-03-25 11:22:04

回答

1

明確地要小心,事情該用戶的輸入字符串:

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

如果通過int()float()(取決於你所期望的輸入來定)更換str(),你問題應該消失,因爲你會得到一個數字類型而不是str ing。

+0

我現在得到這個問題回溯(最近呼叫最後): 文件「\\ CUR-FSM \ 10brierleye $ \ Computing CA 2 \ task 3 average .py「,第57行,在 Stu3AVG =(sum(Score3)/ float(len(Score3)))#定義平均值 TypeError:'int'對象不可迭代 – 2015-03-25 11:24:22

+0

該行不會出現在代碼中你已經發布。真的,你可以a)開一個新的問題或者b)嘗試**理解錯誤的作用。編程是所有關於理解問題,你目前正在做的只是不斷運行到StackOverflow *報告*錯誤。我建議真正閱讀錯誤。它真的說出了什麼問題。 – 2015-03-25 12:26:03

+0

我會盡量向您展示我的整個代碼,但回覆的時間太長了,基本上我輸入一個名稱和一些值來創建輸入分數的平均值,按照字母順序排序名稱並按照數字順序排序從最高到最低,但它說輸入的整數是不可迭代的,我不知道爲什麼:L – 2015-03-26 14:15:41

0

演員串默認輸入到一個整數,這樣就可以計算出平均分:

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

我不斷收到消息:'int'對象不可迭代 – 2015-03-25 11:44:46

+0

您是否正在評分?發佈您的更新代碼進行調試。 – 2015-03-25 11:46:30

+0

你在Python 2或3嗎? – 2015-03-25 11:48:42

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

你也可以請告訴你如何提供輸入。

+0

我通過手動輸入提供輸入,我仍然得到Int不是可迭代的問題,即使我將輸入定義爲整數(因爲我需要在後面的代碼中使用它) – 2015-03-26 14:13:51

+0

你能否聲明行號和錯誤.. – 2015-03-26 16:47:58

+0

行號是56,錯誤是INT是不可迭代的,我不確定爲什麼,因爲它在我設置了值時工作,但現在我添加了以某種方式輸入已打破的值 – 2015-04-15 10:23:50

相關問題