2017-06-26 48 views
0

我從一些基本的東西開始,以獲得如何製作基於文本的遊戲的感覺。在IDE中創建.py文件後,打開終端並使用bash打開.py文件。不能連接「str」和「

hpGanon = 10 
    damage = input("How much damage did you do with the Master Sword?") 
    hpGanon = hpGanon - damage 
    print("Ganon now has " + hpGanon + "hit points.") 

在時,我想它打印結束時,bash告訴我,它不能連接「海峽」和「廉政」的對象。 我試過下面哪些是在下面的文章中說,Python: TypeError: cannot concatenate 'str' and 'int' objects 但我我沒有得到我想要的結果

我只是想說:「Ganon現在有x點生命值。」 任何想法?

+1

嘗試' 「加農現擁有」 + STR(hpGanon)+ 「打點。」' –

+1

你似乎是在Python 2上,不是Python 3. – user2357112

+0

爲什麼你認爲'bash'給你這個錯誤?它應該來自Python。 Bash與運行Python腳本時發生的情況無關。 – Barmar

回答

1

我不完全確定(我從來沒有用Python編碼過),但似乎hpGanon變量是一個整數,而不是一個字符串,所以它不能把它們放在一起。要將整數轉換爲Python中的字符串(我搜索了這個:P),您需要執行str(integer)

所以,在你的榜樣實現這一點,嘗試這樣的事情:

print("Ganon now has " + str(hpGanon) + "hit points.")

+0

@Barmar正如我所說,我從來沒有用Python編碼過。 – Amorris

+0

非常感謝你們,那回答了我的問題:) – alink

相關問題