I may have confused you with the “quotations”. I was just trying to point out the period in the variable name. Your setup for the item is correct. Mimic that approach. Just trade out item for flag.
Like the error tells you, you haven’t defined itemX, your code which you did that in is commented out (lines17-19). If you remove them (the quotes) then your hero will collect the item.
The other problem is the defining of flag.pos (line 9): a better way to do that whole “if flag” section would be to pick up the flag first, then build a fire trap at your pos: (hero.pos.x, hero.pos.y).
# Put flags where you want to build traps.
# When you're not building traps, pick up coins!
while True:
flag = hero.findFlag()
if flag:
# How do we get flagX and flagY from the flag's pos?
# (Look below at how to get x and y from items.)
flag.Pos = flag.Pos.X and flag.Pos.Y
flagX = flag.Pos.X
flagY = flag.Pos.Y
hero.buildXY("fire-trap", flagX, flagY)
hero.pickUpFlag(flag)
else:
item = hero.findNearestItem()
if item:
itemPos = item.pos
itemX = itemPos.x
itemY = itemPos.y
hero.moveXY(itemX, itemY)
Ah, Ok. I realise the level is telling you to do something different from what I’m telling you to do.
The if item section is correct now. Good.
The if flag section should be exactly the same as the if item section (with flag instead of item), except you’re building a fire-trap at the x-y coordinates, rather than moving there. And adding a hero.pickUpFlag(flag) afterwards (which you’ve done).
So let’s look at the two sections which should be identical (except for replacing item with flag):
itemPos = item.pos
itemX = itemPos.x
itemY = itemPos.y
# even if you changed "item" to "flag" there're still differences between the two codes, can you spot them?
flag.Pos = flag.Pos.X and flag.Pos.Y
flagX = flag.Pos.X
flagY = flag.Pos.Y
HINT
Make sure you put the .s in the right places.
And what are you doing with the and?
while True:
flag = hero.findFlag()
if flag:
# How do we get flagX and flagY from the flag's pos?
# (Look below at how to get x and y from items.)
itemPos = item.pos # replace the word item with flag
itemX = itemPos.x # replace the word item with flag
itemY = itemPos.y # replace the word item with flag
hero.buildXY("fire-trap", flagX, flagY)
hero.pickUpFlag(flag)
else:
item = hero.findNearestItem()
if item:
itemPos = item.pos
itemX = itemPos.x
itemY = itemPos.y
hero.moveXY(itemX, itemY)
Here at CodeCombat our goal is to create a fun game that users enjoy that teaches them how to code. Giving the answer away doesn’t teach anything. Please continue to work through your problem. We in turn will continue to give you feedback and support but no direct answers will be given.
If this is an assignment I’m sure your teacher will want you to really know the code and how it works. Not just pass the level to get a good grade.
Continue to try hard and I know U’ll eventually get it!!