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

Help on Usual Day - Backwood Forest - Level 26

while True:
    enemy = hero.findNearestEnemy()
    if enemy and enemy.type == "munchkin":
    hero.attack(enemy)
    hero.attack(enemy)
# Find the nearest item.
item = hero.findNearestItem()
# Collect item if it exists and its type is “coin”.
if item and item.type == "coin":
    hero.moveXY (item.pos.x, item.pos.y)

This is my code and errors keep popping up. Any suggestions?

Please post what the errors are. One of the goals of this level is to keep your code under 8 statements and you have 8, so that goal is not being met. What other errors do you have?

1 Like

It looks like your indentation is incorrect on a few lines. Are you getting this error?

if%20error

2 Likes

Yes - what do I do to fix it?

Also, I can’t collect the coins.

Each coding language has their syntax or structure. Python is very structured around indentation to show placement within a code. Think of it like a structured list like below. You know that the main topic is in line one with the subtopics.

1)
    a.
    b.
        i.
        ii.

Python uses indentation to know what code fits in what groups. When you use a certain type of code, it needs to know what code to run specifically for that group. Loops and If statements are a primary point for indentation. If the indentation is not correct, the code won’t run the way you expect.

while True:
    #everything indented at this level will keep running in the loop
    if this is true:
        then do this because it is indented after the if statement
    else:
        do this if the first check is not true and indented after else
#not indented with the while True loop so it won't run unless you break out of the loop
1 Like