[Solved] Help With Weakest Quickest


#1

Hi,
i’m having a lot of trouble with levels that uses enemy.health or coin.value, Can someone please explain why this is happening?


,My Code:

# Defeat shamans to survive.

# The function find the weakest enemy.
def findWeakestEnemy():
    enemies = hero.findEnemies()
    weakest = None
    leastHealth = 99999
    enemyIndex = 0
    enemies = hero.findEnemies()
    # Loop through enemies:
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        # If an enemy's health is less than leastHealth:
        if enemy.health < leastHealth:    
            # Make it the weakest 
            enemy = weakest
            # and set leastHealth to its health.
            leastHealth = enemy.health
        enemyIndex += 1
    return weakest

while True:
    # Find the weakest enemy with the function:
    weakestShaman = findWeakestEnemy()
    # If the weakest enemy here:
    if weakestShaman:
        # Attack it!
        hero.attack(weakestShaman)


#2

Do weakest=enemy :wink: And weakestShaman=weakest Since there is no such thing as find weakest enemy


#3

Thank you, it worked by replacing enemy = weakest with

weakest = enemy 

But i didn’t Understand why.


#4

Since the other way, it means that the weakest is enemy, but not all enemies are weakest so in the other way the current enemy is the weakest


#5

I’ve never seen this level before. Is this a new release?


#6

Nope, its a desert level, a pretty old one


#7

Okay cuz I’ve never seen the level in the desert before. When I go to desert it never had an arrow pointing at the level until now.

I thought I finished all non-subscriber code combat levels, until today oof


#8

RU a sub or not? Just curious. :wink:


#9

No I’m not a sub :C

Yeah I know it’s sad. But subscribing wouldn’t let me learn new concepts which is why I didn’t subscribe


#10

Hi. i need help. my hero gets killed really fast. what is wrong? please help.

# Defeat shamans to survive.
# The function find the weakest enemy.
def findWeakestEnemy():
    enemies = hero.findEnemies()
    weakest = None
    leastHealth = 99999
    enemyIndex = 0
    # Loop through ene
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        # If an enemy's health is less than leastHealth:
        if enemy.health < leastHealth:
            # Make it the weakest
            weakest = enemy
            # and set leastHealth to its health.
            leastHealth = enemy.health
            enemyIndex += 1 
        return weakest
        

while True:
    # Find the weakest enemy with the function:
    weakest = findWeakestEnemy()
    # If the weakest enemy here:
    if weakest:
        if hero.canCast("invisibility", hero):
            hero.cast("invisibility", hero)
        if hero.canCast("chain-lightning", weakest):
            hero.cast("chain-lightning", weakest)
        else:
            hero.attack(weakest)
    pass


#11

Remove the invisibility and the chain lightning and just use attack. Also can you take a screenshot of your equipment please @Anastasia?


#12


#13

My character doesn’t follow the code and goes and always attacks in the order of bottom first, then right, then left, then up. There is nothing I can do to stop it!


#14

first of all, you don’t have the equipment to cast invisibility, so that shouldn’t even be there. Second, your code is close so the statement, “There is nothing I can do to stop it” is false. The problem is a structural issue in your function. The incremental statement, enemyIndex += 1 needs to be inside the while loop, but not inside the if enemy.health < leastHealth: conditional. Tab it back. Lastly, the return statement needs to be inside the function, but not inside while loop.

Make these corrections and the level will work just fine.


#15

And your equipment should allow the hero to kill the shaman just fine, without the worry of having to cast invisibility and other spells.


#16

I liked the use of the chain lightening here. It allows the hero to kill a shaman without moving all the way to its position. This is helpful here because she’s using Okar, who is really slow.


#17

Thankyou So much, guys!!!