Debug Quiz- How many errors you can find?

These are two functions with some nasty errors but the code runs successfully and the level is passed.

1	def commandTroops():
2	    friends = hero.findFriends()
3	    paladins = hero.findByType("paladin")
4	    enemies = hero.findEnemies()
5	    witches = hero.findByType("witch")
6	    enemies = hero.findEnemies()
7	    ogre = hero.findByType("ogre", hero.findEnemies())
8	    for friend in friends:
9	        if friend.type != 'paladin':
10	            if ogre.health > 0:
11	                hero.command(friend, "attack", "Oni")
12	            elif ogre.health <= 0:
13	                if 'Rusty' :
14	                    hero.command(friend, "attack", "Rusty")
15	                elif 'Skully':
16	                    hero.command(friend, "attack", "Skully")
17	        else:
18	            commandPaladin(friend)
19	
20	def EscapeTimeBoys():
21	    friends = hero.findFriends()
22	    for friend in friends:
23	        if self.now() > 22:
24	            hero.command(friend, "move", {'x': 32, 'y':59})
25	            hero.command(friend, "move", {'x': 50, 'y':57})
26	            hero.command(friend, "move", {'x': 50, 'y':39})
27	        if self.now() > 30:
28	            hero.command(friend, "move", {'x': 78, 'y':40})

  1. Point the error lines and explain why the code isn’t right
  2. Some lines can be deleted without affecting the running of the program - point them
  3. why the lines 10 and 12 don’t throw an error?

/ PS: quiz not for Deadpool198, brooksy125 or jka2706 , but if they want to answer the question 3 they can PM me /
PS: As you may already know, all off-topic addicts are on my ignore list. For example, there are all current Regular’'s If someone is on this list and he / she gives the correct answer to all the questions, the “reward” will be the removal from it

2 Likes

Your first function is not indented right.

the indention is Ok.

I have a question. Now we use hero.time(), right?

Number 18 has an error. There is no commandPaladin() function

He cut the code from its original code.

in line 11, you could replace “Oni” with ogre. You’d still get the same result.

hmm…you’re partially right
Hint: There are 16 lines containing errors (in the two functions) or lines you simply don’t need.
Ex:

3	    paladins = hero.findByType("paladin") # paladins array never used

You’re saying that if there is a string, you command your friends to attack the enemy that has that id? On lines 13 and 15. That means they’re going to attack the enemy Skully always because there is always a string Skully.

Yes, till now 3, 13, 15 are wrong

13	                if 'Rusty' : # this will be always True
15	                elif 'Skully' # this will be always True

I suggest adding this function to help:

def findEnemy(id):
  for enemy in hero.findEnemies():
    if enemy.id == id:
      return enemy   

Replace ‘Skully’ on line 15 with findEnemy(‘Skully’) and then replace ‘Rusty’ on line 13 with findEnemy(‘Rusty’).

1 Like

That’s much, much better… So implementing your idea:

def findEnemy(id):
  for enemy in hero.findEnemies():
    if enemy.id == id:
      return enemy
#####################################################################
            elif ogre.health <= 0:
                Rusty = findEnemy('Rusty')
                Skully = findEnemy('Skully')
                if Rusty :
                    hero.command(friend, "attack", Rusty) # do not attack "String",  it's a bad
                elif Skully:
                    hero.command(friend, "attack", Skully)  # bad idea even if it works :-)

But it doesn’t affect the functioning of the program, so too many errors remain…
PS. i think this function is more versatile:

def findUnit(id, units):
  for unit in units:
    if unit.id == id:
      return unit 
Gorgin = findUnit("Gorgin", hero.findFriends()) # calling the function

Why do you need the “witches” variable?

You use enemies = hero.findEnemies() twice when you don’t need it.

I would like you to change your program commandPaladin(friend) so when the witch is dead, she attacks the skeletons instead of like almost everyone dying.

No, No! It’s not my program - here’s the video of my solution done years ago: player.vimeo.com/video/420089413?
This program is only a tool for refining the debugging skills and a user has passed the level with this code ( I was truly astonished how it’s possible to write something so wrong and to succeed - it’s pure magic :slight_smile:
Yes, you can change the commandPaladin(friend), but first find all the errors in commandTroops().
So till now we have:

def findEnemy(id):
  for enemy in hero.findEnemies():
    if enemy.id == id:
      return enemy

1	def commandTroops():
2	    friends = hero.findFriends()
3	#    paladins = hero.findByType("paladin") not needed
4	#    enemies = hero.findEnemies()  not needed
5	#   witches = hero.findByType("witch")  not needed
6	#   enemies = hero.findEnemies()  not needed
7	    ogre = hero.findByType("ogre", hero.findEnemies())
8	    for friend in friends:
9	        if friend.type != 'paladin':
10	            if ogre.health > 0:
11	                hero.command(friend, "attack", "Oni")
12	            elif ogre.health <= 0: 
13	 #               if 'Rusty' : wrong 
14	 #                   hero.command(friend, "attack", "Rusty") replaced
15	 #               elif 'Skully': wrong
16	 #                   hero.command(friend, "attack", "Skully") replaced
                   Rusty = findEnemy('Rusty')
                   Skully = findEnemy('Skully')
                   if Rusty :
                        hero.command(friend, "attack", Rusty)
                   elif Skully:
                        hero.command(friend, "attack", Skully)  

17	        else:
18	            commandPaladin(friend)

There are still errors but combining them the function works somehow :slight_smile:

I’m confused. How does your program attack all the ogres when your main target is “Oni”?

Somehow, you command too quickly and the archers go to {'x': 78, 'y':40} immediately.

You’re right:

20	def EscapeTimeBoys():
21	    friends = hero.findFriends()
22	    for friend in friends:
23	        if hero.now() > 22:
24	 #          hero.command(friend, "move", {'x': 32, 'y':59}) faulty
25	 #          hero.command(friend, "move", {'x': 50, 'y':57})  faulty
26	            hero.command(friend, "move", {'x': 50, 'y':39})
27	        if hero.now() > 30:
28	            hero.command(friend, "move", {'x': 78, 'y':40})

image
image
lines 24 and 25 aren 't executed
If you don’t know why the current hero.command() gurus are Deadpool and brooksy so serch in their posts.
/I was really amazed by the minions intelligence to go around the wall. I always did it with three-point maneuver :wink: /
But we have 2 more errors in commandTroops(), the first is easy to spot, the second is the cherry on the top

I know this bug. When I first completed this level I had trouble commanding my troops. Then I exploited the bug :smirk: