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

BRIAN WEBER
BRIAN WEBER
21,570 Points

SyntaxError: invalid syntax when I try to print(self.player)

My code looks correct to me, however I am getting a SyntaxError on line 79 when I try to print(self.player). I also noticed that self.setup() should be running first, but I do not see an input for my name or weapon in the console.

Any help would be greatly appreciated!!

Thanks,

Brian

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 got_hit(self):
    self.player.hit_points -= 1

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

      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 point!".format(self.monster))
      self.player.hit_points -= 1
    else:
      print("{} isn't attacking this turn.".format(self.monster))

  def player_turn(self):
    player_choice = input("[A]ttack, [R]est, [Q]uit?").lower()
    if 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.leveledup():
            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()

  def cleanup(self):
    if self.monster.hit_points <= 0:
      self.player.experience += self.monster.experience
      print("You killed {}!".format(self.monster))
      self.monster = self.get_next_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.cleanup()
            print('\n'+'='*20)

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


Game()

1 Answer

Dan Johnson
Dan Johnson
40,533 Points

Just two small syntax errors, I've marked what their issues were with comments:

  def __init__(self):
    self.setup()

    while self.player.hit_points and (self.monster or self.monsters):
      # Missing the closing )
      print('\n' + '=' * 20)
      print(self.player)
      self.monster_turn()
      print('-'*20)
      self.player_turn()
      self.cleanup()
      # Too far indented
      print('\n'+'='*20)

    if self.player.hit_points:
      print("You win!")
    elif self.monsters or self.monsters:
      print("You lose!")
    sys.exit() 
BRIAN WEBER
BRIAN WEBER
21,570 Points

Dan,

Thanks a lot for reviewing my code! I can't believe I didn't notice these mistakes. Everything works as it should now.

Brian