[英]Python: Conditional 'if' statement to set rate
我正在做一個游戲,用戶必須射擊敵人的來襲。
我正在嘗試通過檢查當前分數來提高敵人生成速度的速度。 但是我一直收到錯誤消息。
這是我的代碼:
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
is
是不正確的,雖然可以將比較鏈接在一起,但是您這樣做也是不正確的。
使用任一
if 5 < score < 10:
或(更明確地)
if 5 < score and score < 10:
您可以讓if
-cases級聯,而不是測試每種情況的低值和高值,例如
if score < 5:
spawnrate = 4
elif score < 10:
spawnrate = 6
elif score < 20:
spawnrate = 8
else:
spawnrate = 10
刪除is
,你必須有兩次使用變量and
if score > 5 and score < 10:
spawnrate = 6
elif score > 10 and score < 20:
spawnrate = 8
elif score > 20:
spawnrate = 10
該行:
elif score > 10 and score < 20:
應該 :
elif score > 10 and score < 20:
此外,python允許您執行以下操作:
elif 10 < score < 20:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.