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?
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
Shouldn’t there be a ‘tab’ in the next line of ‘‘if’’?
And your code for collecting items should go inside the while true to keep working