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 trialAleksander Trinh
1,562 Pointsinit problem
So I'm having a problem with my code as after attacking for the second time it doesn't print out any info about monster, player nor wheter the monster decided to attack or not. It just prints it once at the begininng and later on it just asks the same line "Attack, Rest or Quit"
from monster import Goblin
from monster import Troll
from monster import Dragon
import sys
from character import Character
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))
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 ("{} hit you for 1 points".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 are attacking {}".format(self.monster))
if self.player.attack():
if self.monster.dodge:
print ("Attack dodged!")
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")
if player_choice=="r":
self.player.rest()
print('You rest and gain 1 hp!')
if 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.mosnter=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.monster:
print("You lose!")
sys.exit()
Game()
1 Answer
Chris Freeman
Treehouse Moderator 68,454 PointsThe issue is with player_turn.
TL;DR: use elif
instead of if
for input choices.
Looking at the trimmed version below.
def player_turn(self):
player_choice=input("[A]ttack,[R]est,[Q]uit").lower()
if player_choice=="a":
# choice a
if player_choice=="r":
# choice r
if player_choice=="q":
sys.exit()
else:
self.player_turn()
Regardless of what the player chooses if they don't choose Q, else
will rerun the player.
Instead use elif
:
def player_turn(self):
player_choice=input("[A]ttack,[R]est,[Q]uit").lower()
if player_choice=="a":
# choice a
elif player_choice=="r":
# choice r
elif player_choice=="q":
sys.exit()
else:
self.player_turn()