2016-08-01 15 views
-3

對,我在python中創建了一個文本遊戲作爲我的第一個大項目,但我對我的語法感到困惑。它說無效的語法,但我可以看到其他任何東西。所有的幫助提前感謝。製作python文字遊戲無效的語法錯誤

def fight(playerhp, a1, a2, a3, a4, run, d1, d2, d3, d4, enemy, edamage, armor, attack, enemytype, reward): 
print("OH NO YOUVE ENCOUNTERED " + enemy + "WHAT DO YOU DO?") 
time.sleep(1) 
while enemy > 0 and playerhp > 0: 
    attack = input("would you like to " + a1 + a2 + a3 + a4 + "or " + run) 
    if attack == a1: 
     enemy = enemy-d1 
     print("You dealt" + d1 + " damage! the enemy now has " + enemy + "HP!") 
    elif attack == a2: 
     enemy = enemy-d2 
     print("You dealt" + d2 + " damage! the enemy now has " + enemy + "HP!") 
    elif attack == a3: 
     enemy = enemy - d3 
     print("You dealt" + d3 + " damage! the enemy now has " + enemy + "HP!") 
    elif attack == a4: 
     enemy = enemy-d4 
     print("You dealt" + d4 + " damage! the enemy now has " + enemy + "HP!") 
    print("the " + enemytype + "attacks! It deals " + edamage + " damage! you now have" + playerhp + "health!") 
if enemy <=0: 
    print("the monster was slayn and the guts went everywhere :D. In its carcass you found " + reward + "gold!") 
    if playerhp <=0: 
     print("the monster de_stroyed you, and your blood will be painted in its lair.") 

Traceback (most recent call last): 
File "python", line 38 
enemy = enemy - d1 
    ^
SyntaxError: invalid syntax 
+1

您在上一行中缺少分號。應該是'if attack == a1:' – elethan

+0

你在條件結尾的上面一行缺少一個冒號:如果攻擊== a1應該是如果攻擊== a1:。 – idjaw

+0

@Brian Yeah,剛纔也注意到了!其中一些...其實...和父母... – elethan

回答

0

你有很多語法錯誤的...

首先,你忘了該方法的定義後縮進所有代碼。其次,你忘了一個右括號該行代碼,

attack = input("would you like to " + a1 + a2 + a3 + a4 + "or " + run 

接下來,你忘了一個冒號後這個if語句,

if attack == a1 

然後,你忘了很多關閉這些雙引號的HP!後行代碼,

print("You dealt" + d1 + " damage! the enemy now has " + enemy + "HP!) 
print("You dealt" + d2 + " damage! the enemy now has " + enemy + "HP!) 
print("You dealt" + d3 + " damage! the enemy now has " + enemy + "HP!) 
print("You dealt" + d4 + " damage! the enemy now has " + enemy + "HP!) 

最後,這行代碼縮進一個選項卡太多,

print("the monster de_stroyed you, and your blood will be painted in its lair.") 

所以在這裏學到的教訓是要小心並更頻繁地檢查你的代碼。在所有正確的縮進添加後,它應該看起來像這樣,

def fight(playerhp, a1, a2, a3, a4, run, d1, d2, d3, d4, enemy, edamage, armor, attack, enemytype, reward): 
    print("OH NO YOUVE ENCOUNTERED " + enemy + "WHAT DO YOU DO?") 
    time.sleep(1) 
    while enemy > 0 and playerhp > 0: 
     attack = input("would you like to " + a1 + a2 + a3 + a4 + "or " + run) 
     if attack == a1: 
      enemy = enemy-d1 
      print("You dealt" + d1 + " damage! the enemy now has " + enemy + "HP!") 
     elif attack == a2: 
      enemy = enemy-d2 
      print("You dealt" + d2 + " damage! the enemy now has " + enemy + "HP!") 
     elif attack == a3: 
      enemy = enemy - d3 
      print("You dealt" + d3 + " damage! the enemy now has " + enemy + "HP!") 
     elif attack == a4: 
      enemy = enemy-d4 
      print("You dealt" + d4 + " damage! the enemy now has " + enemy + "HP!") 
     print("the " + enemytype + "attacks! It deals " + edamage + " damage! you now have" + playerhp + "health!") 
    if enemy <=0: 
     print("the monster was slayn and the guts went everywhere :D. In its carcass you found " + reward + "gold!") 
    if playerhp <=0: 
     print("the monster de_stroyed you, and your blood will be painted in its lair.") 
+0

好吧,所以我修復了所有這些東西,但錯誤仍然存​​在!它的敵人=敵人 - d1 :( – bobbymg

+0

哦,也沒有結腸的東西是我試圖修復它...... – bobbymg

+0

@bobbymg你可以編輯的問題,包括您的新的和修改的代碼? –