ERROR: Line 29, in Map 'empty_room' :
I have tried rewriting my classes and other small things and have not been able to come up with any solutions. My indentations are correct(or so they seem) on notepad++ they just didn't transfer over very well to SOF. Any troubleshooting is appreciated:) THANK YOU!!
P.S. I am learning on my own with the book 'Learn Python the Hard Way' and I am doing an exercise to make a game similar to zork. Hope this helps.
from sys import exit
class Scene(object):
def enter(self):
print "This scene is not configured"
exit(1)
class Engine(object):
##calling Engine(x) x is the mapScene
def __init__(self, mapScene):
self.mapScene = mapScene
def play(self):
currentScene = self.mapScene.openingScene()
while True:
print "\n-------------------"
nextSceneName = currentScene.enter()
currentScene = self.mapScene.nextScene(nextSceneName)
class Map(object):
scenes = {
'empty_room': EmptyRoom(),
'living_room': LivingRoom(),
'office': Office(),
'hallway': Hallway(),
'kitchen': Kitchen(),
'master_bedroom': MasterBedroom(),
'kids_bedroom': KidsBedroom(),
'attic': Attic()
}
##when calling Map(x) x is the startscene
def __init__(self, startScene):
self.startScene = startScene
##When calling nextScene(x) x is the sceneName
def nextScene(self, sceneName):
return Map.scenes.get(sceneName)
##????
def openingScene(self):
return self.nextScene(self.startScene)
class EmptyRoom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "open door":
return 'living_room'
class LivingRoom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "kitchen":
return 'kitchen'
elif action == "stairs" or "go upstairs":
print "The steps creek as you ascend to the unknown..."
print "Are you sure you want to go up here?"
action = raw_input("> ")
if action == "yes" or "kinda":
return 'hallway'
else:
return 'living_room'
elif action == "empty room":
return 'empty_room'
else:
return 'living_room'
class Kitchen(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "office" or "go right":
return 'office'
elif action == "living room":
return 'living_room'
else:
return 'kitchen'
class Office(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "kitchen":
return 'kitchen'
class MasterBedroom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "hallway":
return 'hallway'
class KidsBedroom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "hallway":
return 'hallway'
class Hallway(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "downstairs" or "stairs" or "living room":
return 'living_room'
elif action == "bedroom" or "master bedroom" or "left":
return 'master_bedroom'
elif action == "kids room" or "kids bedroom" or "right":
return 'kids_bedroom'
elif action == "pull string":
print"You have just opened the attic staircase would you like to go up?"
action = raw_input("> ")
if action == "yes":
return 'attic'
elif action == "no" or "nope":
return 'hallway'
else:
print "I wouldn't have went either\n"
print "SMASH, the attic door springs shut\n"
return 'hallway'
else:
return 'hallway'
class Attic(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "downstairs" or "hallway":
return 'hallway'
aMap = Map('empty_room')
aGame = Engine(aMap)
aGame.play()
The class definition should come first before it is used.
class EmptyRoom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "open door":
return 'living_room'
class Map(object):
scenes = {
'empty_room': EmptyRoom(),
...
}
Same for LivingRoom, Office, Hallway, Kitchen, MasterBedroom, KidsBedroom, Attic.
The definition of scenes inside Map is run when the class is being built, not when you later call the class to instantiate it. At that time, EmptyRoom doesn't exist yet - Python works from the top of your code down, so EmptyRoom only exists after it has reached the end of the indented block underneath class EmptyRoom:. So for this to work, Map needs to go after all of your other classes rather than before.
Related
I've defined the following:
class SMSMessage(object):
def __init__(self, hasBeenRead, messageText, fromNumber):
self.hasBeenRead = hasBeenRead
self.messageText = messageText
self.fromNumber = fromNumber
hasBeenRead = "False"
fromNumber = "07189202003"
With the following functions:
def MarkAsRead(self):
if hasBeenRead == "False":
return hasBeenRead == "True"
def add_sms():
sms1 = (hasBeenRead, messageText, fromNumber)
return SMSStore.append(sms1)
def get_count():
return len(SMSStore)
def get_message(i):
for i in SMSStore:
return messageText
def get_unread_messages(i):
for i in SMSStore:
if hasBeenRead == "False":
return messageText
This is the Logic for the SMS simulation where a user is meant to send messages to a list (SMSStore[ ]) and then recall specific messages from the list:
userChoice = ""
while userChoice != "quit":
userChoice = raw_input("What would you like to do - read/send/quit?")
if userChoice == "read":
unreadChoice = raw_input("Would you like to retrieve all unread messages or one of your own choice? - all unread/custom ")
if unreadChoice == "custom":
messageNo = int(raw_input("Please enter which messsage number you want to read: "))
print get_message(messageNo)
print MarkAsRead(messageNo)
elif unreadChoice == "all unread":
print get_unread_messages(hasBeenRead)
else:
print "incorrect entry"
elif userChoice == "send":
messageText = raw_input('Please type in your message')
add_sms()
print SMSStore
My main issue is that I am able to send the (hasBeenRead, messageText, fromNumber) to SMSStore but when trying to read I can't seem to return the messagetext of the user selected messageNo. It always returns the messageText of the last item in the list. I'm still new to coding so any help would be greatly appreciated.
Thanks
I'm not sure what is the role of SMSStore, but this block of code looks suspicious to me:
def get_message(i):
for i in SMSStore:
return messageText
Why are you iterating the SMSStore but just to return messageText ? Would that be a missing Indent over there ?
I am fairly new to Python and I am learning how classes and methods interact by creating a text-based game. The code runs great, however, my question lies in the Name(object) class. Wherever I put it in the code, (in the beginning or in the middle as shown below) I will always be prompted with an input. I suspect it may be because of the attribute name_new that prompts raw_input("> "), however, I was wondering if there was a way to prevent or change when the Class Name() is prompted.
class Character(object):
def __init__(self, name, weapon, spec_ability, health):
self.name = name
self.weapon = weapon
self.spec_ability = spec_ability
self.health = 100
def special_ability(self):
special = False
rand_num = str(random.choice(rdm))
if '1' in rand_num or '2' in rand_num or '3' in rand_num:
special = True
if special:
print "Your special ability is ready."
return True
else:
return
else:
print "Your special ability is not ready."
def Critical_Chance(self):
crit = False
rand_num = str(random.choice(rdm))
if '1' in rand_num or '2' in rand_num or '3' in rand_num:
crit = True
if crit:
print "Critical hit incoming."
return True
else:
return
class Warrior(Character):
def __init__(self, name, weapon, spec_ability, health, armor):
super(Warrior, self).__init__(name, weapon, spec_ability, health)
self.armor = armor
class Mage(Character):
def __init__(self, name, weapon, spec_ability, health, cloak):
super(Mage, self).__init__(name, weapon, spec_ability, health)
self.cloak = cloak
class Rogue(Character):
def __init__(self, name, weapon, spec_ability, health, boots):
super(Rogue, self).__init__(name, weapon, spec_ability, health)
class Name(object):
name_new = raw_input("> ")
def start():
print """\nWelcome to the world of _______. \n
You are a young hero or heroine
in search of the gold that hides within the Mountains of _____.
In order to obtain the gold and treasure that lies within the
mountains, you must battle great monsters and face dangerous
perils."""
print "Will you pursue such adventure? Or will you refuse?"
choice = raw_input("> ")
if "Yes" in choice or "yes" in choice:
Introduction()
elif "No" in choice or "no" in choice:
sys.exit()
else:
print "You typed: %s. \nPlease try again." % choice
start()
def Introduction():
print "Thank you for joining in on this adventure..."
print "Well, welcome to your adventure for the gold."
print "You will encounter dangerouse quests and enemies, but you will be\
rewarded in the end."
print "Now, what class will you be?\n\
Warrior Mage Rogue"
char_choice = raw_input("> ")
verynew_name = Name()
name_choice = Name.name_new
if "Warrior" in char_choice or "warrior" in char_choice:
name_choice = Warrior(name_choice, None, None, None, None)
print name_choice.name
print name_choice.weapon
print name_choice.spec_ability
print name_choice.health
print name_choice.armor
elif "Mage" in char_choice or "mage" in char_choice:
name_choice = Mage(name_choice, None, None, None, None)
print name_choice.name
print name_choice.weapon
print name_choice.spec_ability
print name_choice.health
print name_choice.cloak
elif "Rogue" in char_choice or "rogue" in char_choice:
name_choice = Rogue(name_choice, None, None, None, None)
print name_choice.name
print name_choice.weapon
print name_choice.spec_ability
print name_choice.health
print name_choice.boots
else:
print "You must pick a class."
start()
print "Our story starts in the town of Larton."
#super(Larton, self).enter()
start()
When you run your program, the module-level code is executed. This includes your Name class.
class Name(object):
name_new = raw_input("> ")
This creates the class Name and also executes the code within the class. Which is why you're prompted to enter a name.
If you want to be able to prompt the user for a name like this
verynew_name = Name()
you have to change your Name class so that raw_input is executed when you instantiate the class (because that's what Name() does, it creates an instance of Name):
class Name(object):
def __init__(self):
self.name_new = raw_input("> ")
(Note that instead of name_choice = Name.name_new you'll have to use name_choice = verynew_name.name_new.)
define constructors for your Character class and subclasses that don't take a name as a paramter:
e.g.
class Warrior(Character):
def __init__(self, weapon, spec_ability, health, armor):
super(Warrior, self).__init__(weapon, spec_ability, health)
self.armor = armor
and in character class, you can define self.name to be None by default
You can always assign the name later on in the logic, by using something like
character.name = Name.name_new
As long as you do that before you actually read the value of name, you will be fine
I'm really stuck on this simple user input game I created. It's an exercise from learn python the hard way. I've been trying for over a week to figure it out on my own and finally caved in to asking for help. I think the problem point in my code is the Engine() class. Anyway here's the code:
from sys import exit
from random import randint
class Island(object):
def enter(self):
pass
class Engine(object):
def __init__(self, island_map):
self.island_map = island_map
def play(self):
current_island = island_map.opening_island()
last_island = self.island_map.next_island('Tropical')
while current_island != last_island:
next_island_name = current_island.enter()
current_island = self.island_map.next_island(next_island_name)
current_island.enter()
class Loser(Island):
snippets = ["Welcome to loser Island",
"Can't win them all",
"There's always next time"]
def enter(self):
print "Game over"
print Loser.snippets[randint(0,len(self.snippets)-1)]
exit(1)
class Jungle(Island):
def enter(self):
print "Welcome to the Jungle!"
print "You must fight the monkey to pass!"
print "Choose your weapon..."
weapons = ['stick', 'fly swatter', 'banana']
print weapons
fighter_weapon = raw_input("Choose from a a weapon above...")
if fighter_weapon == 'stick':
print "Stick can't beat a monkey!"
print "Monkey wins"
return 'Loser'
elif fighter_weapon == 'fly swatter':
print "The monkey steals your fly swatter"
print "Monkey wins"
return 'Loser'
elif fighter_weapon == 'banana':
print "The monkey is hypnotized by the banana"
print "You continue to the next island..."
return 'Frozen'
else:
print "What? Doesn't make sense"
return 'Jungle'
class Frozen(Island):
#add green, blue circle, and black diamond
def enter(self):
print "This is snow land"
print "Here's your snowboard, choose your path"
print "([[[[[[[[[)"
runs = ["green", "blue", "black"]
print runs
run_choice = raw_input(" >")
if run_choice == 'green':
print "Easy way out?"
print "No good"
print "Wrong"
return 'Loser'
elif run_choice == 'blue':
print "okay, at least not the easiest way"
print "You have some guts, I'll let you choose one more time"
return 'Frozen'
elif run_choice == 'black':
print "I like your style"
print "Going for the hard choice shows courage"
print "Continue to the next island"
return 'Tropical'
else:
print "Say whaaat?!"
return 'Frozen'
class Tropical(Island):
def enter(self):
print "You made it to the final Island!"
print "Look here's the treasure chest!"
print " All that's left to do is guess the code on the chest"
print "Be smart, you only get five guesses..."
t_key = "1234"
chest_guess = raw_input("What do you think it is?")
guesses = 0
while chest_guess != t_key and guesses < 4:
print "Guess again"
guesses += 1
chest_guess = raw_input(" ?")
if chest_guess == t_key:
print "You guessed right!"
print "You won"
else:
print "Sorry"
return 'Loser'
class Map(object):
islands = {
'Loser': Loser(),
'Jungle': Jungle(),
'Frozen': Frozen(),
'Tropical': Tropical(),
}
def __init__(self, start_island):
self.start_island = start_island
def next_island(self, island):
val = Map.islands.get(island)
return val
def opening_island(self):
return self.next_island(self.start_island)
mj_map = Map('Jungle')
mj_game = Engine(mj_map)
mj_game.play()
Right now I'm stuck on an error that states "island_map" is not defined in my Engine class. Which I don't understand because to me it looks like it is defined. Any input would be appreciated.
def play(self):
current_island = island_map.opening_island()
needs to be changed to
def play(self):
current_island = self.island_map.opening_island()
as it is apart of that instance
I am making a small text game, and one of my methods is behaving unexpectedly, and I can't figure out why. If I import the module and run the method in python, it works fine. Some parts of the game aren't complete, but that parts that I need run fine.
With the module pasted below, choose method is changing a list into a string, but only when the module is run. I'm running this on 2.7.3, on a mac.
Edit: I figured it out. The program execution had continued, and I was confused by the choose call in the match method. The options parameter sent in match was a string, instead of the list it should have been, but that was simple to figure out why.
import fighters
import weapons
from random import randint
from sys import exit
class Hero(object):
weapons = ["spikeglove", "shortsword"]
hp = {"actual":50, "base":50}
score = 0
def attack(self, weapon, method):
return None
class Scene(object):
# Returns one or more items from the provided choices
def choose(self, options, max_choices, prompt):
chosen = []
while len(chosen) < max_choices and len(options) > 0:
print
print prompt
print options, chosen
for o in options:
print "- {}".format(o)
print type(options)
choice = raw_input("> ")
if choice in options:
chosen.append(choice)
options.remove(choice)
print options, chosen
else:
print "That is not one of the options!"
if len(chosen) == 1:
return chosen[0]
else:
return chosen
class Death(Scene):
def enter(self):
print "You are now dead"
return "menu"
class Exit(Scene):
def enter(self):
print "Thanks for playing!"
return exit(1)
class Menu(Scene):
def enter(self):
menu_options = ["play", "train", "exit"]
choice = self.choose(menu_options, 1, "Welcome to the Arena!")
return choice
class Level(Scene):
def __init__(self):
self.prologue = "Welcome to {}".format(self.__class__.__name__)
next_scene = "level1"
# A match is the fight between the Hero and a particular Enemy
# A Hero must defeat each Enemy via a match to win the tournament/level
def match(self, hp, weapons, enemy):
hero_hp = hp
enemy_hp = enemy.stats["hp"]
last_attack = ""
while hero_hp and enemy_hp:
weapon_choice = self.choose(weapons, 1, "> ")
attack_choice = self.choose(weapon_choice.available_attacks(last_attack), 1, "> ")
last_attack = attack_choice
hero_attack = hero.attack(attack_choice)
enemy_attack = enemy.attack()
print "Hero: {} {}: {}".format(hero_hp, enemy, enemy_hp)
if hero_hp:
return "victory", hero_hp
else:
return "defeat"
# The game cycles through the enemies that our hero has to face
# passing the setup to the "match" function for the fighting
def enter(self):
print
print
print self.prologue
start_hp = hero.hp['actual']
weapons = self.choose(hero.weapons, 2, "Choose a weapon")
while self.enemies:
enemy = fighters.roster[self.enemies.pop()]
match_outcome, finish_hp = self.match(start_hp, weapons, enemy)
if match_outcome == "defeat":
hero.hp['actual'] = hero.hp['base']
return "death"
# The Hero enters the next match with the current HP obtained from the previous match
start_hp = finish_hp
# Add our Hero's victory points, and get them some medical attention
# before heading back to the Barracks
points_award = 5
hero.points += points_award
hero.hp['actual'] = hero.hp['base']
new_weapon = "shortsword"
hero.add_weapon(new_weapon)
epilogue = """Congratulations!
You have gained {} points for a total score of {}.\n
You have unlocked the \"{}\" """.format(
points_award, hero.points, new_weapon)
return next_scene
class Train(Scene):
pass
class Level1(Level):
enemies = ["boxer", "boxer"]
next_scene = "level2"
class Level2(Level):
pass
class Level3(Level):
pass
class Level4(Level):
pass
class Level5(Level):
pass
class Engine(object):
def __init__(self, start_scene):
self.start_scene = start_scene
scenes = {
"menu": Menu(),
"death": Death(),
"exit": Exit(),
"play": Level1(),
"level2": Level2(),
"level3": Level3(),
"level4": Level4(),
"level5": Level5(),
}
def next_scene(self, scene_name):
return self.scenes[scene_name]
def play(self):
current_scene = self.scenes[self.start_scene]
while True:
next_scene = current_scene.enter()
current_scene = self.scenes[next_scene]
if __name__ == "__main__":
hero = Hero()
game = Engine("menu")
game.play()
Terminal output:
$ python arena.py
Welcome to the Arena!
['play', 'train', 'exit'] []
- play
- train
- exit
<type 'list'>
> play
['train', 'exit'] ['play']
Welcome to Level1
Choose a weapon
['spikeglove'] []
- spikeglove
<type 'list'>
> spikeglove
[] ['spikeglove']
>
spikeglove []
- s
- p
- i
- k
- e
- g
- l
- o
- v
- e
<type 'str'>
Yet when I run in python:
>>> import arena
>>> s = arena.Level1()
>>> s.choose(['shortsword', 'spikeglove'], 2, "Choose a weapon")
Choose a weapon
['shortsword', 'spikeglove'] []
- shortsword
- spikeglove
<type 'list'>
> shortsword
['spikeglove'] ['shortsword']
Choose a weapon
['spikeglove'] ['shortsword']
- spikeglove
<type 'list'>
> spikeglove
[] ['shortsword', 'spikeglove']
['shortsword', 'spikeglove']
The if __name__ == "__main__": means that the portion of code indented after that if-statement will be executed only when the script is executed, not when it is imported.
Working code is here: http://codepad.org/L0OkeHlk
from sys import exit
class Map(object):
def __init__(self, start):
self.start = start
def play(self):
next = self.start
while True:
print "\n---------"
room = getattr(self,next)
next = room()
def death(self):
exit(1)
def redroom(self):
prompt = raw_input("> ")
if "white" in prompt:
return "whiteroom"
elif "blue" in prompt:
return "blueroom"
else:
print "DOES NOT COMPUTE (RED ROOM)"
return 'death'
def whiteroom(self):
prompt = raw_input("> ")
if "red" in prompt:
return "redroom"
elif "blue" in prompt:
return "blueroom"
else:
print "DOES NOT COMPUTE (WHITE ROOM)"
return 'death'
def blueroom(self):
prompt = raw_input("> ")
if "white" in prompt:
return "whiteroom"
elif "red" in prompt:
return "redroom"
else:
print "DOES NOT COMPUTE (BLUE ROOM)"
return 'death'
class Engine(object):
pass
x = Map("redroom")
x.play()
The above works. But only because I'm doing everything in one class. I want to take the play and death functions and put them in a class of their own.
My problem comes from two places. How do I get the Engine class to call the functions in the map class to make the game work? How do I know what to initialize in the Map/Engine class? I get that I'm supposed to initialize something but I have no idea how to come up with it. What should be my first thought when deciding what to initialize?
Non-functioning code (my best attempt): http://codepad.org/ELlaHQiB
from sys import exit
class Map(object):
def redroom(self):
prompt = raw_input("> ")
if "white" in prompt:
return "whiteroom"
elif "blue" in prompt:
return "blueroom"
else:
print "DOES NOT COMPUTE (RED ROOM)"
return 'death'
def whiteroom(self):
prompt = raw_input("> ")
if "red" in prompt:
return "redroom"
elif "blue" in prompt:
return "blueroom"
else:
print "DOES NOT COMPUTE (WHITE ROOM)"
return 'death'
def blueroom(self):
prompt = raw_input("> ")
if "white" in prompt:
return "whiteroom"
elif "red" in prompt:
return "redroom"
else:
print "DOES NOT COMPUTE (BLUE ROOM)"
return 'death'
class Engine(object):
def __init__(self, start):
self.start = start
def play(self):
next = self.start
while True:
print "\n---------"
room = getattr(self,next)
next = room()
def death(self):
exit(1)
x = Engine("redroom")
x.play()
Also, I'm unsure of the play function. It works but only because I got it from the tutorial I was reading. What is the purpose of the next variable becoming self.start and then becoming room() in the while loop?
This is what I would do.
class Engine(object):
def __init__(self,rooms):
self.rooms = rooms
self.current = 0
def next(self):
self.rooms[self.current]()
self.current += 1
if len(self.rooms) == self.current:
self.current = 0
# or whatever logic you want.
m = Map()
rooms = [m.redroom, m.whiteroom, m.blueroom]
e = Engines(rooms)
e.next() # should call redroom of class Map
e.next() # should call whiteroon etc