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!
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

Christopher Wommack
3,983 PointsGame.py
i keep getting invalid syntax error on the else: print('you missed')
<p>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.monsters = 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))
if input('Do you want to 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('{} 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 are attacking {}'.formt(self.monster))
if player.attack():
if monster.dodge():
print('{} dodged your attack'.format(self.monster()))
else:
if self.player.levelup():
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 = get_next_monster()
def __init__(self):
self.setup()
while self.player.hit_ploints and (self.monster or self.monsters)
print('\n'+'='*20)
print(self.player)
self.monster_turn()
print('-'*20)
self.cleanup()
print('\n'+'='*20)
if self.player.hit_points:
print('You win')
elif: self.monsters or self.monster:
print('You lose')
sys.exit()
Game() </p>
2 Answers

Steven Parker
225,652 PointsYou have 3 else statements for one if.
An if statement can have only one else associated with it. Could some of the indentation be off?
You also have a couple of elif that have no if just after that print('you missed')
.
Plus all the elif statements have a stray colon between the keyword and the condition expression.

Christopher Wommack
3,983 PointsOk I think I corrected all those errors but I am still getting a invalid syntax on the else: print('You missed)
<p>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.monsters = 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.monsters))
if input('Do you want to 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('{} 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 are attacking {}'.format(self.monster))
if self.player.attack():
if self.monster.dodge():
print('{} dodged your attack'.format(self.monster()))
else:
if self.player.levelup():
self.monster.hit_points -= 2
else:
self.monster.hit_points -= 1
print('You hit {} with your {}'.format(self.monster, self.player.weapon))
elif:
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.monsters = get_next_monster()
def __init__(self):
self.setup()
while self.player.hit_points and (self.monster):
print('\n'+'='*20)
print(self.player)
self.monster_turn()
print('-'*20)
self.cleanup()
print('\n'+'='*20)
if self.player.hit_points:
print('You win')
elif self.monster:
print('You lose')
sys.exit()
Game() </p>

Steven Parker
225,652 PointsA few things stand out for me at first glance:
- an elif must have a conditional expression (just like an if) — note that you have some correct ones later
- an else must come last, after any and all elif statements associated with the same if
- the original tests that included "
self.monsters or self.monster
" were probably right and should be restored - the original indentation of "
sys.exit()
" was also correct and should be restored - there's a stray HTML tag on the first and last line
I think that funny elif might actually have been intended as an else, but you meant to associate it with the previous if. You probably just indented it (and the following print statement) one stop too far.