Sarven Sum noob


#1

hi!
actually i have a very weak understanding about what’s going on with my code =) especially i’m always messing up with spacing and index incrementing. can someone tell me the way to improve this code. this is everything that i know for this moment, but i’ve spied a part of solution on a github.

loop:
    whiteX = {'x':27, 'y':42}
    items = self.findItems()
    traps = self.findByType("fire-trap")
    trapsIndex = 0
    min = 9999
    max = 0
    enemies = self.findEnemies()
    enemyIndex =0
    while trapsIndex < len(traps):
            trap = traps[trapsIndex]
            if trap.value < min:
               min = trap.value
            if trap.value > max:
               max = trap.value 
    trapIndex += 1
    value = min + max   
    self.move(whiteX)
    self.say(value)
             
    if enemyIndex < len(enemies):
             enemy = enemies[enemyIndex]
             enemyIndex += 1
             if enemy:
                self.attack(enemy)

#2

shouldn’t this:

if enemyIndex < len(enemies):

be this?

for enemyIndex < len(enemies):

#3

oh, there should be while, no? i have not yet learned for


#4

You use “for” in the code above… and it is formatted incorrectly. It looks like that should be a “while”. I am not sure what you are doing with trap values either.

Your spacing under the current for loop and the if enemyIndex look wrong.


#5

I’m edited my code, but still need help


#6

@Alex_Captain You need to increment your index within the while loop:

    while trapsIndex < len(traps):
        # blabla
        trapIndex += 1         # note the indentation!

Furthermore, this part doesn’t need to be in a loop - you have to do it only once. The loop should start after that. Structure your code somehow like this:

# find the min and max values
# go to white X
# say the result

loop:
    # if there is an enemy
        # fight it
    # otherwise
        # move towards the exit

One last word of advice: the easiest way to fight enemies is to find the nearest one and attack it (if there is one).

Happy coding!