2016-01-23 42 views
0

我正在製作一個遊戲,用戶必須拍攝新一波敵人。Python:有條件的'if'語句來設置速度

我試圖通過檢查當前得分來提高敵人產卵的速度。但是我不斷收到錯誤。

這裏是我的代碼:

for x in range(score): 

    if score is > 5 and < 10: 
      spawnrate = 6 
    elif score is > 10 and < 20: 
      spawnrate = 8 
    elif score is > 20: 
      spawnrate = 10 
+2

什麼錯誤?你會收到錯誤信息 - 然後添加全文。 – furas

+2

爲什麼你使用'for'循環? – furas

+0

如果'score'等於10或20,應該發生什麼? – SiHa

回答

2

is是不正確的,而有可能鏈比較起來,你正在做的錯誤也是如此。

二者必選其一

if 5 < score < 10: 

或(更明確地)

if 5 < score and score < 10: 
0

刪除is,你必須與and

if score > 5 and score < 10: 
     spawnrate = 6 
elif score > 10 and score < 20: 
     spawnrate = 8 
elif score > 20: 
     spawnrate = 10 
0

行兩次使用變量:

elif score > 10 and score < 20: 

應該是:

elif score > 10 and score < 20: 

此外,Python允許你做這樣的事情:

elif 10 < score < 20: 
1

,而不是測試每一種情況下低值和高值,可以讓if - 例如級聯,如

if score < 5: 
    spawnrate = 4 
elif score < 10: 
    spawnrate = 6 
elif score < 20: 
    spawnrate = 8 
else: 
    spawnrate = 10