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 trialjoleneoropilla
17,214 PointsHow do I resolve multiple errors in my code and get my game running?
I've completed Object-Oriented Python, but can't seem to resolve the multiple errors in my Monsters game.
Any help would be appreciated. Please find the list of errors and my game.py code below. I've also included my monster.py code since the error list recalls that file.
Errors (began after inputting weapon):
Name: Jolene
Weapon ([S]word, [A]xe, [B]ow): S
Traceback (most recent call last):
File "game.py", line 88, in <module>
Game()
File "game.py", line 70, in __init__
self.setup()
File "game.py", line 12, in setup
Troll(),
File "/home/treehouse/workspace/monster.py", line 17, in __init__
self.max_hit_points)
File "/usr/local/pyenv/versions/3.4.1/lib/python3.4/random.py", line 2
18, in randint
return self.randrange(a, b+1)
File "/usr/local/pyenv/versions/3.4.1/lib/python3.4/random.py", line 1
96, in randrange
raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart
, istop, width))
ValueError: empty range for randrange() (3,2, -1)
game.py:
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 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.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!")
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.monster:
print("You lose!")
sys.exit()
Game()
monster.py:
import random
from combat import Combat
COLORS = ['yellow', 'red', 'blue', 'green']
class Monster(Combat):
min_hit_points = 1
max_hit_points = 1
min_experience = 1
max_experience = 1
weapon = 'sword'
sound = 'roar'
def __init__(self, **kwargs):
self.hit_points = random.randint(self.min_hit_points,
self.max_hit_points)
self.experience = random.randint(self.min_experience,
self.max_experience)
self.color = random.choice(COLORS)
for key, value in kwargs.items():
setattr(self, key, value)
def __str__(self):
return "{} {}, HP: {}, XP: {}".format(self.color.title(),
self.__class__.__name__,
self.hit_points,
self.experience)
def battlecry(self):
return self.sound.upper()
class Goblin(Monster):
max_hit_points = 3
max_experience = 2
sound = 'squeak'
class Troll(Monster):
min_hit_points = 3
max_hit_poitns = 5
min_experience = 2
max_experience = 6
sound = 'growl'
class Dragon(Monster):
min_hit_points = 5
max_hit_points = 10
min_experience = 6
max_experience = 10
sound = 'raaaawr'
1 Answer
Dan Johnson
40,533 PointsLooks to be just a particularly nasty typo in that it got covered by the class variables:
class Troll(Monster):
min_hit_points = 3
# Typo: max_hit_poitns = 5 -> max_hit_points = 5
max_hit_points = 5
min_experience = 2
max_experience = 6
sound = 'growl'
joleneoropilla
17,214 Pointsjoleneoropilla
17,214 PointsDan Johnson, that was the culprit. Still can't believe I didn't see it. Thanks!
Follow-up... were you able to find that typo based on the errors?
Dan Johnson
40,533 PointsDan Johnson
40,533 PointsI followed the stack trace:
You'll notice the last call dealing with your code is the constructor call for Troll, so I looked closer for typos since it's a pretty simple class with its logic defined by the base class.