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 Object-Oriented Python (retired) Hack-n-Slash The Final Push

Gilang Ilhami
Gilang Ilhami
12,045 Points

gamel.py is not running

When i tried to execute the script, it doesn't work I already wrote Game() at the end

import sys

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

class Game:


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

    self.monster = self.get_next_monster()

  def get_next_monster(self):
    try:
      return self.mosnter.pop(0)
    except IndexError:
      return None

    def monster_turn(self):
      if self.mosnter.attack():
        print ("{} is attacking".format(self.mosnter))

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

    def player_turn(self):
      player_choice = input("[A]ttack, [R]est, or [Q]uit").lower()
      if self.player_choice == 'a':
        print ("You're 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're attacking {} with your {}.".format(
                self.mosnter, self.weapon))
        else:
          print ("You missed")

      elif self.player_choice == 'r':
        self.player_rest()
      elif self.player_choice == 'q':
        sys.exit()

      else:
        self.player_turn()
      # Let the player attack, rest, or quit
      # If they attacks:
        # See if the attack is successful
          # If so, see if the mosnter dodges
            # If dodged, print that
            # If not, subtract right num of hit points from the monster
          # If not a good attack, tell the player
      # If the player rest:
        # Call the plyer.rest() method
      # If the quit, exit the game
      # If they pick anything else, rerun this method

    def cleanup(self):
      if self.mosnter.hit_points <= 0:
        self.player.experience += self.monster.experience
        print ("You killed the monster")
        self.monster = self.get_next_monster()
      # If the mosnter has no more hit points:
        # Up the players experience
        # Print message
        # Get a new mosnter

    def __inti__(self):
      self.setup()

      while self.player.hit_points and (self.mosnter or self.monster):
        print ('\n'+'='*20)
        print (self.player)
        self.monster_turn
        print ('-'*20)
        self.player_turn
        self.cleanup
        print ('\n'+'='*20)

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


Game()

Could you post the error message that Python gave you in the Terminal?

Or if you are doing this inside TeamTreehouse Workspaces you can share your Workspace and I can Fork it and try to run it myself. It will give you a public link when you share it. :)

Jacob Hill
Jacob Hill
2,805 Points

Try setting something equal to game instead.

player = Game()

1 Answer

Check your indentation and typos -- from quick glance, it looks like you spelled init as inti. Could be as simple as that :)

Matt Beck

Changed to answer. Good catch.

Also note there are a few other typos throughout the code, such as:

self.mosnter.attack(): # self.mosnter should be self.monster