简体   繁体   中英

Python: Conditional 'if' statement to set rate

I'm making a game where the user must shoot an incoming wave of enemies.

I am trying to increase the speed of the rate at which the enemies spawn by checking the current score. However I keep getting an error.

Here is my code:

 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 is incorrect, and while it is possible to chain comparisons together, you are doing that incorrectly as well.

Use either

if 5 < score < 10:

or (more explicitly)

if 5 < score and score < 10:

Rather than testing each case for low and high values, you can let the if -cases cascade, like

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

Remove is and you have to use variable twice with and

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

The line :

elif score > 10 and score < 20:

Should be :

elif score > 10 and score < 20:

In addition, python allows you to do something like this :

elif 10 < score < 20:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM