2015-02-07 144 views
-2

當我嘗試運行此代碼時,出現語法錯誤,但未說明它是什麼行。我不知道我能說些什麼..這裏是代碼,獲得誤差位:當我嘗試運行此代碼時,出現語法錯誤

if "q" in attack: 
     if random.randint(1,100) != range(1,21): 
      print("You hit with a quick attack!") 
      ehp -= 20 
      print("The",enam,"loses 20 damage! It now has",ehp,"health.") 
     else: 
      print("You missed.. :(") 
    elif "p" in attack: 
     if random.randint(1,100) != range(1,51): 
      print("You hit with a power attack!") 
      ehp -= 50 
      print("The",enam,"loses 50 damage! It now has",ehp,"health.") 
     else: 
      print("You missed.. :(") 
    elif "1" in attack: 
     if mana >= skill1[2]: 
      print("You hit with",skill1[0]) 
      ehp -= skill1[1] 
      mana -= skill1[2] 
      print("The",enam,"loses",skill1[1],"damage! It now has",ehp,"health.") 
      print("You now have",mana,"mana.") 
    elif "2" in attack: 
     if mana >= skill2[2]: 
      print("You hit with",skill2[0]) 
      ehp -= skill2[1] 
      mana -= skill2[2] 
      print("The",enam,"loses",skill2[1],"damage! It now has",ehp,"health.") 
      print("You now have",mana,"mana.") 
    elif "3" in attack: 
     if mana >= skill3[2]: 
      print("You hit with",skill3[0]) 
      ehp -= skill3[1] 
      mana -= skill3[2] 
      print("The",enam,"loses",skill3[1],"damage! It now has",ehp,"health.") 
      print("You now have",mana,"mana.") 
    else: 
     print("You typed something wrong.") 

順便說一句,skill1,skill2和skill3是在比賽中我要作不同的技能所有列表技能1 [0]是技能的名稱,技能[1]是技能的攻擊力和技能[2]是用來使用技能的法力值。

skill1 = [] 
skill2 = [] 
skill3 = [] 

skill1.append("Very Weak Fireball") 
skill1.append(20) 
skill1.append(30) 
skill2.append("Weak Fireball") 
skill2.append(30) 
skill2.append(40) 
skill3.append("Average Fireball") 
skill3.append(40) 
skill3.append(50) 
+0

哪個語法錯誤?它通常給你的行號是錯誤的,這在這裏會非常有用。但是既然你說的沒有,確切的錯誤確實會變得更有趣 – 2015-02-07 10:07:50

+0

請確保你正確地縮進了你的代碼(第一個'if'語句) – vaultah 2015-02-07 10:09:06

+0

請注意你的縮進.. – Olu 2015-02-07 10:14:12

回答

0

不能嵌套的,如果裏面的elif:

if "q" in attack: # in line with the elif's 
    if random.randint(1,100) > 21: # cannot compare to range use > 
     print("You hit with a quick attack!") 
     ehp -= 20 
     print("The",enam,"loses 20 damage! It now has",ehp,"health.") 
    else: 
     print("You missed.. :(") 
elif "p" in attack: 
    if random.randint(1,100)> 51: # greater that 51 
     print("You hit with a power attack!") 
     ehp -= 50 
     print("The",enam,"loses 50 damage! It now has",ehp,"health.") 
    else: 
     print("You missed.. :(") 
elif "1" in attack: 

您的代碼語法的其餘部分都很好,只是改變範圍>

0

你的代碼有太多問題。例如:

if random.randint(1,100) != range(1,21): 

你在這裏做什麼比較列表(範圍的輸出,這是[1,2, ..., 20)一個整數(1到100之間的隨機數)。你的意思可能是not (... in range(...));這是可以的,但是檢查數字是否在兩個其他數字之間的時間和內存消耗最多。但是,這不是語法錯誤。

但是,這裏的要點是,你不能正確縮進;您的elif必須具有與相應的if相同的縮進深度。

0

空閒的輸出對我來說:

unindent does not match any outer indentation level

你沒有使用正確的壓痕。

檢查以下:

if "q" in attack: 
    if random.randint(1,100) != range(1,21): 
     print("You hit with a quick attack!") 
     ehp -= 20 
     print("The",enam,"loses 20 damage! It now has",ehp,"health.") 
    else: 
     print("You missed.. :(") 
elif "p" in attack: 
    if random.randint(1,100) != range(1,51): 
     print("You hit with a power attack!") 
     ehp -= 50 
     print("The",enam,"loses 50 damage! It now has",ehp,"health.") 
    else: 
     print("You missed.. :(") 
elif "1" in attack: 
    if mana >= skill1[2]: 
     print("You hit with",skill1[0]) 
     ehp -= skill1[1] 
     mana -= skill1[2] 
     print("The",enam,"loses",skill1[1],"damage! It now has",ehp,"health.") 
     print("You now have",mana,"mana.") 
elif "2" in attack: 
    if mana >= skill2[2]: 
     print("You hit with",skill2[0]) 
     ehp -= skill2[1] 
     mana -= skill2[2] 
     print("The",enam,"loses",skill2[1],"damage! It now has",ehp,"health.") 
     print("You now have",mana,"mana.") 
elif "3" in attack: 
    if mana >= skill3[2]: 
     print("You hit with",skill3[0]) 
     ehp -= skill3[1] 
     mana -= skill3[2] 
     print("The",enam,"loses",skill3[1],"damage! It now has",ehp,"health.") 
     print("You now have",mana,"mana.") 
    else: 
     print("You typed something wrong.") 

後,如果您沒有定義攻擊,您將收到另一個錯誤:

NameError: name 'attack' is not defined

如果攻擊是一個字符串不是一個變量,您必須將其替換爲「攻擊」(加引號)

由於@Padraic坎寧安在評論中說,攻擊顯然是個變數!所以你必須定義它。 :)

+0

q怎麼會發生「攻擊「? – 2015-02-07 10:22:44

+0

@PadraicCunningham它不能! :)等python不運行在其塊中的代碼!我再次檢查!它不會運行任何塊! :D – Jean 2015-02-07 10:27:27

+0

那麼代碼將永遠不會運行,我認爲這是安全的假設攻擊是一個變量;) – 2015-02-07 10:28:31

相關問題