**Help Please** Rich Forager in Python with Mahogany Glasses

Here’s my code:

# Use "if" and "else if" to handle any situation.
# Put it all together to defeat enemies and pick up coins!
# Make sure you bought great armor from the item shop! 400 health recommended.

loop:
    flag = self.findFlag()
    enemies = self.findEnemies()
    items = self.findItems()


    if flag:
        # What happens when I find a flag?
        self.pickUpFlag(flag)
        
    elif enemies:    
         # What happens when I find an enemy?
        for enemy in enemies:
            if self.isReady("cleave"):
                self.cleave(enemy)
            else:
                    self.attack(enemy)   
    else:            
       for item in items:
                if item:
                    position = item.pos
                    x = position.x
                    y = position.y
                    self.moveXY(x, y)
                    self.say("Money!")

Only the first part of my code is executing. I can place flags and attack enemies both cleave and attack work but he won’t pick up items automatically. I also reversed my enemy and item statements then I could place flags and automatically pick up items but he wouldn’t attack enemies or cleave. When I run the check-marks do not go beyond the second if statement. Please help.

Tip:
Enclosing your code with three backticks presents it in a nice way:

```
#some code
```

becomes

#some code

I did this for you this time


I can see no immediate flaw in your code, except some mixed up indentations (which could be it). Try to move self.attack(enemy) and everything below for item in items: (but not the for itself) 4 spaces to the left (remove one level of indentation).

Tell us if this worked.

You also probably don’t want to do your own for-in loops on this level, because you won’t be able to respond to new enemies and items. You can just attack one enemy once, or just move to the nearest coin. Use enemy = self.findNearest(self.findEnemies()) and item = self.findNearest(self.findItems()).

Thank you for your responses, I was able to fix it with adding self.findnearest() to each statement. I bought the mahogany glasses to beat “Deadly Dungeon Rescue”, hopefully using self.findFriend() . I’m not sure how to make the tortured peasant follow me, any ideas? Thanks in advance!

loop:
    flag = self.findFlag()
    enemies = self.findEnemies()
    items = self.findItems()
    friends = self.findFriends()
   
    if flag:
        # What happens when I find a flag?
        self.pickUpFlag(flag)
        
    elif self.findNearest(enemies):    
         # What happens when I find an enemy?
        for enemy in enemies:
            if enemies > 3:
                if self.isReady("cleave"):
                    self.cleave(enemy)
            else:
                self.attack(enemy)   
    elif self.findNearest(items):           
       for item in items:
            if item:
                position = item.pos
                x = position.x
                y = position.y
                self.moveXY(x, y)
                
    elif self.findNearest(friends):
        for friend in friends:
        
            self.follow(friend)

Thanks i appreciate the tip.

I believe, you have to leave the room before he will follow you.

Thanks that was my problem.

elif enemies:

This isn’t checking to see if there are enemies, this is checking to see that the enemies variable is not None.

If you changed

enemies = self.findEnemies()

to read

enemies = None

then the if check would fail and your character would start picking up coins.

When dealing with arrays like this, it is common to perform a check like this:

if enemies and len(enemies) > 0:

To first see if enemies is a valid value and then check to see if the collection has anything in it.


Now what @Nick was referring to in his post. With the way you’re coded now, if there were 1000 coins on the screen and no enemies, your character would start collecting the coins and wouldn’t stop until they had all been gathered. Even if more enemies appear, your character would be stuck in the loop over the coins and wouldn’t be able to respond to the enemies. Your character would continue to collect coins until they were all gone or your character was killed, whichever happens first.

I feel it’s best to do one thing each time through a loop. Figure out the one most important thing to do and do it. After that’s been done, conditions may have changed. The second most important thing in the previous loop may not be the best course of action anymore.

   for item in items:
        if item:
            position = item.pos
            x = position.x
            y = position.y
            self.moveXY(x, y)

Collecting all of the coins may not be the best course of action when new enemies appear. :smile:

Thanks for the input. I don’t understand, if i set enemies to None and:

if enemies and len(eniemies) > 0:

My hero does nothing.

Also how would i change my finding items code to prioritize enemies over coin? I’ve been working on the “Keeping Time”. This is exactly what I need to do.

“len(eniemies)” enemies is misspelt

Here’s my code in more detail. It’s not a misspell, the game checks for undefined variables.

# Use your new skill to choose what to do: self.now()


enemy = None
coin = self.findNearestItem()



loop:
    # If it's the first 10 seconds, fight.
    if self.now() < 10:
        if enemy and len(enemy) > 0:
            self.findNearestEnemy()
            self.attack(enemy)

    # Else, if it's the first 30 seconds, collect coins.
    else:
            self.moveXY(coin.pos.x, coin.pos.y)
    
    # After 30 seconds, join the raid!

My hero does nothing, what am I doing wrong? He jitters for a bit then stands still.

The bit about “enemies = None” really only does what he was talking about with the code you posted before he made that comment. Now that you have changed to the code to let the “loop:” and the “ifs” handle everything it doesn’t apply…

You need to put the setting of enemy and coin into the loop. Outside the loop they only happen once, so you would never get more than one coin.

the self.finddNearestEnemy() inside the if would need a “enemy=” or it does nothing for you.

loop:
    enemy = self.findNearestEnemy()
    coin = self.findNearestItem()

    # If it's the first 10 seconds, fight.
    if self.now() < 10:
        if enemy:
            self.attack(enemy)

    # Else, if it's the first 30 seconds, collect coins.
    else:
        self.moveXY(coin.pos.x, coin.pos.y)

    # After 30 seconds, join the raid!

The double indent you had in front of the “move to get coin” command may also have been a problem (it can be picky about such things). If you want them to line up combine the first two ifs

    if self.now() < 10 and enemy:
        self.attack(enemy)

there is no difference since there is nothing else in the “if now < 10” part of the if.

1 Like

Great! Thanks for the tip.