> Tutorial
A list lets you store several values in a specific order.
You can create a list with the following syntax:
my_list = [1, 2, 3, 4, 5]
The first position in a list is position 0. You access a specific element of the list with:
my_list[0] # Gets the first element
my_list[2] = 10 # We assign the value 10 to the 3rd element
a = my_list[3] + my_list[4] # we add the 4th and 5th elements and store the result in the variable a
Many operations are available for working with lists. The most notable ones are:
len(my_list) # size of the list
my_list.append(x) # adds an element x at the end
my_list.insert(pos, x) # inserts an element x at a position
my_list.pop(pos) # removes the element at a position
x in my_list # True if the element x is in the list
my_list.sort() # sorts the list
for in loopYou can iterate over each of the values in a list using a for loop with range, like this:
my_list = [10, 20, 30, 40, 50]
for i in range(len(my_list)):
print(my_list[i])
But there is a simpler way to iterate over each of the values in a list: the for in loop. The following code does the same as the previous one, but more concisely:
my_list = [10, 20, 30, 40, 50]
for element in my_list:
print(element)
Our current AI has a problem: when the enemy is almost dead, our leek uses its boosts, heals and shields before using its weapon, when it could have finished the enemy off!
To improve this end-of-fight situation, we'll estimate the damage we can deal to the enemy, to know whether we can finish it off.
We start by getting the list of our weapon's features with me.weapon.features:
effects = me.weapon.features # list of features
total = 0 # We'll add up all the damage
for effect in effects: # we iterate over all the features
Each feature has properties: .type (the effect type), .minValue (minimum value), .maxValue (maximum value)... We calculate the final damage when the feature is damage:
if effect.type == Effect.DAMAGE: # if it's damage
min_damage = effect.minValue # minimum damage of the weapon
boosted_damage = min_damage * (1 + me.strength / 100) # increased by strength
final_damage = boosted_damage * (1 - enemy.relativeShield / 100) - enemy.absoluteShield # reduced by shields
We add the damage to the total:
total += final_damage
We finish with a condition: if the total damage is greater than the enemy's life, we're going to kill it, so we attack! Otherwise, we hold back and use chips:
total_damage = total * (me.tp // me.weapon.cost) # multiplied by the number of possible uses (TP / cost of the weapon)
if total_damage < enemy.life:
# We can't finish it :( heals, boosts, shields
pass
else:
# We can finish it! Attack
pass
> Tip: you can extract a range from a list with my_list[2:8].
> Tip: lists can be nested: a list inside a list [ [1, 2, 3] ], and you can access a list inside a list: my_list[0][0].
What can a list be used for?
Storing several values Sorting values Running instructions in parallel Performing several calculations
Which pieces of code are valid?
a = [1, 2, 3] a[0] + a[2] [1, 2, 3] = a len([1, 2, 3]) [ [1, 2], [3, 4], [5, 6] ] my_list[5][10]
me = Fight.me
# We take the pistol if it isn't equipped
if me.weapon is None:
me.setWeapon(Weapon.pistol)
# We get the nearest enemy
enemy = Fight.getNearestEnemy()
# We greet our opponent
if Fight.turn == 1:
me.say("Hi " + enemy.name + "!")
# We move toward the enemy
me.moveToward(enemy)
effects = me.weapon.features # list of features
total = 0 # We'll add up all the damage
for effect in effects: # we iterate over all the features
if effect.type == Effect.DAMAGE: # if it's damage
min_damage = effect.minValue # minimum damage of the weapon
boosted_damage = min_damage * (1 + me.strength / 100) # increased by strength
final_damage = boosted_damage * (1 - enemy.relativeShield / 100) - enemy.absoluteShield # reduced by shields
total += final_damage
total_damage = total * (me.tp // me.weapon.cost) # number of possible uses (TP / cost of the weapon)
if total_damage < enemy.life:
# We can't finish it :( Boosts, shield and heal
me.useChip(Chip.motivation)
me.useChip(Chip.protein)
me.useChip(Chip.helmet)
# We heal ourselves if our life is below our max life.
if me.life < me.maxLife:
me.useChip(Chip.bandage)
# We try to shoot at it 4 times!
for i in range(4):
me.useWeapon(enemy)
Impossible de charger les données du jeu.
Vérifiez votre connexion et réessayez.