lundi 3 février 2020

Python challenge:zombies

I am trying to solve the following challenge, but to no avail: The zombies start at range metres, and move at 0.5 metres per second. Each second, you first shoot one zombie, and then the remaining zombies shamble forwards another 0.5 metres.

If any zombies manage to get to 0 metres, you get eaten. If you run out of ammo before shooting all the zombies, you'll also get eaten. To keep things simple, we can ignore any time spent reloading.

Write a function that accepts the total number of zombies, a range in metres, and the number of bullets you have.

If you shoot all the zombies, return "You shot all X zombies." If you get eaten before killing all the zombies, and before running out of ammo, return "You shot X zombies before being eaten: overwhelmed." If you run out of ammo before shooting all the zombies, return "You shot X zombies before being eaten: ran out of ammo."

(If you run out of ammo at the same time as the remaining zombies reach you, return "You shot X zombies before being eaten: overwhelmed.".)

My code thus far is:

def zombie_shootout(zombies, distance, ammo):
    if ammo >= zombies:
        ammo -= 1
        zombies -= 1
        distance -= 0.5
    elif ammo < zombies:
        print("You shot ",zombies,"zombies before being eaten: ran out of ammo.")
    elif distance == 0:
        print("You shot ",zombies,"zombies before being eaten: overwhelmed.")
    else:
        print("You shot all ", zombies,"zombies.")

I know there are solutions for those who can't solve this puzzle, but they are most likely much more succinct and elegant, and I would like to know how (if it is possible at all) to go about doing it with my way (lots of ifs and elifs and maybe add a while somewhere).

Aucun commentaire:

Enregistrer un commentaire