Useful Competitors

Can anybody please explain why my code doesn’t work for this level. Also, by the way whatever I do, I am unable to get my indentations to turn blue? Any help please?

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        if not enemy.type is "peon":
            hero.attack(enemy)
    item = hero.findNearestItem()
    if item:
        itemx = item.Pos x
        itemy = item.Pos y
        if item.type != "poison":
            hero.moveXY(itemx, itemy)

One issue I see is definitely here:

    itemx = item.Pos x       # should be: item.pos.x
    itemy = item.Pos y       # should be: item.pos.y

Cheers

2 Likes

Thank you, it worked. So for future reference, locating an item without it’s specific coordinates I would use code with the format of “name.pos.x , name.pos.y”?

@jackattack825 If I understand you correctly. You are asking if one doesn’t receive actual coordinates of an item in the form x = 30 and y = 45 can you use “name.pos.x” to find its x coordinate?

Yes.

As long as that game piece has a “pos” attribute. For instance the Hero has a “pos” enemies have a “pos”, doors may have a “pos”. The game should throw an error if the game piece doesn’t have a “pos” and you type “name.pos.x”.

Friends, Enemies, and items should generally have a “pos” And everything that has a “pos” has an “x” and “y” component.

I ran your code and it worked perfectly fine. Have you let it run all the way through? If there is a peon it goes and gets the poison and dies but if not you attack then go get coins.

Help! My code is didn’t work. Maybe it’s a bug?
while True:
enemy = hero.findNearestEnemy()
if enemy:
if enemy.type != “peon”:
hero.attack(enemy)
item = hero.findNearestItem()
if item:
# Собери предмет только если тип (type) не равен “poison”.
if item.type != “poison”:
pos = item.pos
x = pos.x
y = pos.y
hero.moveXY(x, y)
pass

First please format your code correctly by placing triple backticks around it and adding python to the top one.

1 Like

I have the same problem, I can’t get past Useful Competitors level, and I have tried both Python and JavaScript.

My Python looks like this:

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        if not enemy.type is "peon":
            hero.attack(enemy)
    item = hero.findNearestItem()
    if item:
        itemx = item.pos.x
        itemy = item.pos.y
        if item.type != "poison":
            hero.moveXY(itemx, itemy)

I can defeat all the enemies but I’m left with one gem every time.

I used the same code as s1mpsonn in the beginning, and that didn’t work either.

The Python was formatted correctly, but for some reason when I press Reply it “unformats”

See the FAQ for code formatting tips. I’ve fixed it for you this time.

Your code is working for me, maybe you got a bad seed. Try resubmitting the code (click “Submit”) to see if it helps.

1 Like

Thanks Alex!

I now have a sword and fix the code. That’s what worked in my python (sorry if I posted the answer, feel free delete it)

[redacted, please don’t post solutions]

oh man, it ruins all indentation again!

Check the FAQ for code formatting tips. And yeah, please don’t post solutions. :slight_smile:

1 Like

I used the same code, and it doesn’t work for me. Help!

Here is my code.
while True:
enemy = hero.findNearestEnemy()
if enemy:
if not enemy.type is “peon”:
hero.attack(enemy)
item = hero.findNearestItem()
if item:
itemx = item.pos.x
itemy = item.pos.y
if item.type != “poison”:
hero.moveXY(itemx, itemy)

Please like @UltCombo said format your code according to the FAQ.
https://discourse.codecombat.com/faq

I just can’t get my program to work. Tell me the answer

Dude this topic is dead. Notice on the top it reads just before your post 5 MONTHS LATER!!!

[don’t post solutions]

i tried doing it using javascript but it won’t work

while(true) {
enemy = hero.findNearestEnemy()
if (enemy) {
if (not enemy.type == “peon”): {
hero.attack(enemy)
item = hero.findNearestItem()
if item:
itemx = item.Pos.x
itemy = item.Pos.y
if item.type != “poison”:
hero.moveXY(itemx, itemy)
}
}

}

this is what poped up

this

any help?