Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Can't spot the mistake: The Final Push/Dragon game

Good evening,

I have really now checked my code with the code Kenneth Love used in this video but mine isn't working and I can't find the mistake and I am really comparing the code snippets. So maybe four eyes will see more than two:)

https://teamtreehouse.com/library/objectoriented-python/hacknslash/the-final-push

Thanks in Advance

And that's my code:

import sys
from character import Character
from monster import Dragon
from monster import Goblin
from monster import Troll

class Game:
  def setup(self):
    self.player=Character()
    self.monsters=[
      Goblin(),
      Troll(),
      Dragon()
    ]
    self.monster=self.get_next_monster()

  def get_next_monster(self):
    try:
      return self.monsters.pop[0]
    except IndexError:
      return None

    def monster_turn(self):
      if self.monster.attack():
        print("{} is attacking!".format(self.monster))
        # check to see, if the monster attacks
        # if so, tell the player

        if input("Dodge? Y/N").lower()=="y":
          # check if the player wants to dodge
          if self.player.dodge():
            # if so, see if the dodge is succesful
            # if it is, move on
            print("You dodged the attack.")
          else:
            print("You got hit anyway.")
            self.player.hit_points-=1
            # if it's not, remove one player hit_point
        else:
          print("{} hit you for 1 point!".format(self.monster))
          self.player.hit_points-=1
      else:
        print("{} isn't attacking this turn.".format(self.monster))
        # if the monster isn't attacking, tell that to the playere too

    def player_turn(self):
        player_choice=input("[A]ttack, [R]est, [Q]uit? ").lower()
        if player_choice=="a":
          print("You are attacking {}".format(self.monster))

          if self.player.attack():
            if self.monster.dodge():
              print("{} dodged your attack.".format(self.monster))
            else:
              if self.player.leveled_up():
                self.monster.hit_points-=2
              else:
                self.monster.hit_points-=1

              print("You hit {} with your {}".format(self.monster, self.player.weapon))

          else:
            print("You missed!")
        elif player_choice=="r":
          self.player.rest()
        elif player_choice=="q":
          sys.exit()
        else:
          self.player_turn()





        # let the player attack, rest or quuit
        # if they attack:
            # see if the attack is succesful
                # if so, see if the monster dodges
                  # if dodged, print that
                  # if not dodged, subtract the right num of hit_points from the monster
                # if not a good attack, tell the player
        # if they rest:
            # call the player.rest() method
        # if they quit, exit the game
        # if they pick anythin else, re-run this method


    def cleanup(self):
        if self.monmster.hit_points<=0:
           # if the monster has no more hit_ponts
          self.player.experience+=self.monster.experience
          # up to the player's experience

          print("You killed {}!".format(self.monster))
        # print a message

        self.monster=self.get_next_monster()
        # Get a new monster

    def __init__(self):
        self.setup()

        while self.player.hit_points and (self.monster or self.monsters):
          print("\n"+"="*20)
          print(self.player)
          self.monster_turn()
          print("-"*20)
          self.player_turn()
          self.clean_up()
          print("\n"+"="*20)


          if self.player.hit_points:
            print("You win!")
          elif self.monsters or self.monster:
            print("You loose!")
          sys.exit()



Game()

2 Answers

Steven Parker
Steven Parker
243,215 Points

At first glance, I spotted a likely misspelling:

        if self.monmster.hit_points<=0:

Then I noticed brackets where I expected parentheses:

      return self.monsters.pop[0]

Finally, I saw a reference to "self.clean_up()" but the definition was for "cleanup()" (no underscore).

If that's not the whole problem, perhaps you could describe what symptom(s) you are experiencing with it.

Thanks, I fixed those parts. But it still doesn't work. Because when I run this, nothing happens, the game won't start.

I have solved it on my own. (A few intendation problems)