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
Gilang Ilhami
12,045 Pointsgame.py doesn't run on workspace
I think all of my code is very clear and there is nothing wrong with it but when i tried to run it on work space, nothing appears
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:
self.monster.pop(0)
except IndexEerror:
return None
def monster_turn(self):
if self.monster.attack():
print ("{} is attacking".format(self.monster))
if input("Dodge? Y/N").lower() == 'y':
if self.monster.dodge():
print("You dodged the attack")
else:
print("You got hit anyway")
self.player.hit_points -= 1
else:
("{} hit you for 1 point".format(self.monster))
else:
print("{} is currently not attacking".format(self.monster))
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.monster, 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()
def cleanup(self):
if self.monster.hit_points <= 0:
self.player.experience += self.monster.experience
print ("You killed the 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.monster or self.monsters:
print("You lose")
sys.exit()
Game()
I tried re writing it and watch the video again 2 times, i still don't get it
3 Answers
Ryan S
27,276 PointsHi Gilang,
It might be because your last command 'sys.exit()' is outside of the __init__ function. I think the Game is exiting as soon as you run it.
sys.exit() should only execute when the while loop is done and the "You win" or "You lose" if conditions are met. Try tabbing it in and see if that helps.
Good luck.
Gilang Ilhami
12,045 PointsHey Ryan, thank you very much for the answer. I did what you recommended me to do, but there is no result to it
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.monster or self.monsters:
print("You lose")
sys.exit()
Ryan S
27,276 PointsHi Gilang,
After reviewing your code some more, I noticed that the indentation for all but your first method (setup()) is off. The way you have your code right now is that every method is actually indented inside the setup() method. So you will need to fix that such that every method definition is aligned properly. This is the main reason why your program isn't running.
The other thing is that I found quite a few syntax errors, typos, and missing keywords while troubleshooting your code, but I won't list them here. It'll be good practice for you to try and debug them yourself using the errors that Python will throw at you. The major hurdle was actually getting the program to run in the first place. But if you get stuck again I'll be happy to help you with it.
But just to get you started, aside from fixing the indentation of all your methods, don't forget to use parentheses when calling your methods in __init__
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() # add parentheses
print('-'*20)
self.player_turn() # add parentheses
self.cleanup() # add parentheses
print('\n'+'='*20)
if self.player.hit_points:
print("You win!")
elif self.monster or self.monsters:
print("You lose")
sys.exit()
Good luck.