Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[solved] Continuous Alchemy Help please

while True:
enemy = hero.findNearestEnemy()
item = hero.findNearestItem()

# If there is no enemy, continue out of the loop.
if not enemy:
    hero.moveXY(hero.pos.x, hero.pos.y)
    continue

# If there is no item, ask for a potion then continue:
if not item:
    hero.say("Give me a drink!")
    hero.moveXY(hero.pos.x, hero.pos.y)
    continue

# If the item.type "poison", continue out of the loop.
if item.type == "poison":
    hero.moveXY(hero.pos.x, hero.pos.y)
    continue
# At this point, the potion must be a bottle of water
# so moveXY to the potion, then back to the start!
hero.moveXY(44, 35)

Im new to code combat and I have been looking through forums for help but my code still doesn’t work. Can I get some help?

Hi Zabe and welcome to the forum!

I see a couple of issues. First, not sure why you have hero.moveXY(hero.pos.x, hero.pos.y) twice…if you omit these lines, the code will still work properly.

Second, at the end, you should add an else: statement, then move to the good potion pos, and then move back to the starting X.

Still don’t think I fully understand what to do. My hero goes to the potion spot and doesn’t move.

The two moveXY statements I referred to don’t really do anything…they tell the hero to move to his current position, ie. to remain in place

One thing I didn’t notice at first, your ‘enemy =’ and ‘item =’ lines don’t appear to be indented, but I assume that was because you simply didn’t include them when you copy/pasted your code.

Your final hero.move statement is outside of the ‘while true’ loop…it will not execute until ‘while true’ = False, which will never happen. You were also missing a statement telling your hero to move to the item.pos.x and .y…I assume you added that, but you’ll need to post your code again if you are still having issues.

while True:
enemy = hero.findNearestEnemy()
item = hero.findNearestItem()

# If there is no enemy, continue out of the loop.
if not enemy:
    continue

# If there is no item, ask for a potion then continue:
if not item:
    hero.say("Give me a drink!")
    continue

# If the item.type "poison", continue out of the loop.
if item.type == "poison":
    
# At this point, the potion must be a bottle of water
# so moveXY to the potion, then back to the start!

I reset the code to try to fix it but im still lost

Ok, in your final if block…if the item.type == “poison”, you want to do nothing (continue); else, you want to move to the potion’s position and then finally return to the starting X.

This help any?

yes! thank you so much!