2013-05-08 71 views
0

我不斷收到此錯誤我感到困惑的這個錯誤

TypeError: unsupported operand type(s) for +: 'int' and 'str' 

在我下面的代碼:

done = False 
while not done: 
    if You.Hit_points > 0 and Opponent.Hit_points > 0: 
     move = raw_input("Would you like to make a move? (y/n) ") 
     if move == "y": 
      print "",You.name,"hit ",Opponent.name," by",You.Hit_points," hit points!" 
      Opponent.Health = You.Hit_points + You.Skill_points + Opponent.Health 

謝謝!

+11

再次閱讀錯誤。真的很慢。 – 2013-05-08 04:01:14

+0

你給一個字符串添加一個數字,不允許。要麼所有的數字或所有的字符串。 – 2013-05-08 04:01:35

回答

1

Hit_points可能是一個int。將其轉換爲字符串:

str(You.Hit_points) 

編輯:

等待,沒有。 Misread,Nolen Royalty是正確的。這可能就足夠了:

Opponent.Health=int(You.Hit_points)+int(You.Skill_points)+int(Opponent.Health) 

但是我會遵循Nolen的建議。

+0

他正在計算對手的健康狀況。這大概是一個整數本身,而不是三個字符串的連接。 – Cairnarvon 2013-05-08 04:02:39

+0

是的,我誤讀了。編輯。 – 2013-05-08 04:05:33

4

Opponent.Health,You.Hit_pointsYou.Skill_points中的至少一個是一個字符串,至少它們是一個數字(一個int)。你正試圖將字符串和數字加在一起。如果你打算把所有這些數值都作爲數字,你需要弄清楚哪一個不是,並且改變它。您可以將所有值都轉換爲int,但這是一個短期解決方案,如果您不修復這個問題,這個問題會不斷出現。

您需要的所有信息都在錯誤中:unsupported operand type(s) for +: 'int' and 'str'