3 Replies - 331 Views - Last Post: 18 January 2013 - 03:53 AM Rate Topic: -----

#1 NoRemorse  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 11

Class name / Import problem

Posted 07 January 2013 - 07:16 AM

the_game.py is like follows:
from sys import exit
from random import randint
from game_room import *

class Hallway(object):
	"""\tYou are in a warehouse with 2 doors.
	Which one do you try to open?
	Number 1 or 2?
	"""

	def run(self):
	
		door = raw_input("> ")
	
		if door == "first" or door == "1": 
			next_room = Puzzle_room()
			return next_room
		elif door == "second" or door == 2:
			next_room = death()
			return next_room
		else:
			print "DO NOT COMPUTE!"
			exit(0)
		
	
class Puzzle_room(object):
	"""\tPuzzles, puzzles, puzzles, oh the lovely puzzles.
	I hope you like them. Here is one:
	Which of the three boxes in front of you has the key to
	the next room? And which one has a poisonous snake in it
	and which one has a grenade in it? Choose wisely:
	"""
	
	def run(self):
		box = raw_input("Box #: ")
	
		if box == "1" or box == "one":
			print "\tYou chose the right box! Congratz!"
			print "\tGo explore some more!"
			next_room = Empty_room()
			return next_room
		elif box == "2" or box == "two":
			print "\tOh nooo, the grenades splint is removed..."
			print "\tand you got blow into pieces. That is sad!"
			print "\tGame over!"
			next_room = death()
			return next_room
		elif box == "3" or box == "three":
			print "\tYaaay, the snake is sleeping and you try to open another box"
			print "\tAs soon as you move the snake wakes up and screams:"
			print "\tHello, muthafucka! And then he bites you. After which you"
			print "\ttwist and turn in agonising pain for 10 minutes and die"
			print "\tNice JOB!"
			next_room = death()
			return next_room
		else:
			print "\tDo not COMPUTE!"
			next_room = Puzzle_room()
			return next_room
		
		
class Monster_room(object):
	"""\tYou have entered a dark room. In the center of the room
	is a fire. Above the fire is a sign. The sign says "Feed
	the beast". What do you do?
	"""
	
	def run(self):
	
	
		the_beast = raw_input("I will: ")
	
		if "run" in the_beast:
			print "\tYou chicken! You run around for hours and die of dehydration."
			next_room = death()
			return next_room
		elif "wait" in the_beast:
			print "\tYou wait for 3 hours and die of boredom"
			print "\tNice job!"
			next_room = death()
			return next_room
		elif "feed" in the_beast:
			print "\tYou are brave, young badavan!"
			print "\tYou take the meat from the feed box and wave it around"
			print "\tFinally a little chihaua or smt that looks like a rat"
			print "\tcomes to you. You throw it the meat and it gives you the"
			print "\tkey to the next room because it is so intelligent, but"
			print "\tcan not feed itself."
			next_room = Basement()
			return next_room
		else:
			print "\tDo not COMPUTE!"
			next_room = Monster_room()
			return next_room
		
	
class Basement(object):
	"""\tYou go down the stairs and into the basement.
	The basement has another door. The door has
	a keypad. You need to hack the massive 1 digit code.
	"""
	
	def run(self):
	
	
		code = "%d" % (randint(1,5))
		guesses = 0
		guess = raw_input("[Keypad]: ")
	
		while guess != code and guesses < 4:
			print "Try again:"
			guesses += 1
			guess = raw_input("[Keypad]: ")
	
		if guess == code:
			print "\tYay, good for you. You will not die of boredom."
			print "\tRight here right now anyway."
			print "\tYou see a misterious glow from behind another door"
			print "\tSice you got big cojones you open the door and enter."
			next_room = Gold_room()
			return next_room
		else:
			print "\tWhether you do not know your numbers or just suck"
			print "\tYou can not crack the code and die of hunger."
			next_room = death()
			return next_room
		
		
class Gold_room(object):
	"""\tYou enter a room that is made entirely out of gold.
	You have to go over a suspicious looking bridge in orde
	to get out of the warehouse. There is some gold on floor.
	How many bars do you take?
	"""
	
	def run(self):
		gold = int(raw_input("# of bars: "))
	
		if gold < 10:
			print "\tCongratz, you win! You got out of the warehouse."
			exit(0)
		else:
			print "\tYou greedy mofo!"
			print "\tThe bridge collapses beacause you took to much!"
			print "\tEnjoy beeing dead."
			next_room = death()
			return next_room
		
		
class death(object):
	"""\tYou are dead.
	"""
	
	def run(self):
	
		exit(0)
		


	
class runner(object):

	def __init__(self, start):
		self.start = start
		
	def play(self):
		next_room = self.start
			
	
		while True:
			print "---------------" * 4
			print next_room.__doc__
			next_room = next_room.run()
		
			
first_room = Hallway()

game = runner(first_room)

game.play()



game_room.py , which is just to use another file, contains 1 simple room / class
class Empty_room(object):
	"""\tThe room is totally empty.
	There is a crayon on the floor.
	Draw yourself a door to get out
	of this place.
	"""
	
	def run(self):
		print "\tThat is one ugly ass door."
		next_room = Monster_room()
		return next_room

When I run it, and get to the end of the Empty_room, Which should direct me to Monster_room, I get a name error:
global name Monster_room is not defined

I guess it is probably a simple solution to this problem, but I can not figure it out, since I do import
evrything from game_room.py so it should work, but obviously does not. Can someone help me? Any help is appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Class name / Import problem

#2 npanagakis  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 01-December 12

Re: Class name / Import problem

Posted 07 January 2013 - 10:36 AM

Im not sure if this would fix it, but you should probably try using:
import game_room.py


Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Class name / Import problem

Posted 07 January 2013 - 10:48 AM

You know, all that text in your class definitions doesn't really do much for you... Ah, I see, you're using __doc__ as a hack.

Your problem is that Empty_room doesn't know about Monster_room. It's all alone with no imports. You are using objects. You can change how they behave. Make empty room take a parameter. Also, make a base class and perhaps move the doc hack to where your runner needn't worry about it.

I fiddled with this a little:
class Room(object):
	"""
	You see nothing
	"""

	def show(self):
		print "\t" + self.__doc__

	def run(self):
		return None

class Empty_room(Room):
	"""The room is totally empty.
	There is a crayon on the floor.
	Draw yourself a door to get out
	of this place.
	"""
	def __init__(self, next_room):
		Room.__init__(self)
		self.next_room = next_room
	
	def run(self):
		print "\tThat is one ugly ass door."
		return self.next_room


class Hallway(Room):
	"""You are in a warehouse with 2 doors.
	Which one do you try to open?
	Number 1 or 2?
	"""

	def run(self):
		door = raw_input("> ")
		if door == "first" or door == "1": 
			return Puzzle_room()
		if door == "second" or door == 2:
			return death()
		return Forward("Do not COMPUTE!")

		
	
class Puzzle_room(Room):
	"""Puzzles, puzzles, puzzles, oh the lovely puzzles.
	I hope you like them. Here is one:
	Which of the three boxes in front of you has the key to
	the next room? And which one has a poisonous snake in it
	and which one has a grenade in it? Choose wisely:
	"""
	
	def run(self):
		box = raw_input("Box #: ")
	
		if box == "1" or box == "one":
			print "\tYou chose the right box! Congratz!"
			print "\tGo explore some more!"
			return Empty_room(Monster_room())
		elif box == "2" or box == "two":
			print "\tOh nooo, the grenades splint is removed..."
			print "\tand you got blow into pieces. That is sad!"
			print "\tGame over!"
			return death()
		elif box == "3" or box == "three":
			print "\tYaaay, the snake is sleeping and you try to open another box"
			print "\tAs soon as you move the snake wakes up and screams:"
			print "\tHello, muthafucka! And then he bites you. After which you"
			print "\ttwist and turn in agonising pain for 10 minutes and die"
			print "\tNice JOB!"
			return death()
		else:
			return Forward("Do not COMPUTE!", self)
		
		
class Monster_room(Room):
	"""You have entered a dark room. In the center of the room
	is a fire. Above the fire is a sign. The sign says "Feed
	the beast". What do you do?
	"""
	
	def run(self):
		the_beast = raw_input("I will: ")
		if "run" in the_beast:
			print "\tYou chicken! You run around for hours and die of dehydration."
			return death()
		elif "wait" in the_beast:
			print "\tYou wait for 3 hours and die of boredom"
			print "\tNice job!"
			return death()
		elif "feed" in the_beast:
			print "\tYou are brave, young badavan!"
			print "\tYou take the meat from the feed box and wave it around"
			print "\tFinally a little chihaua or smt that looks like a rat"
			print "\tcomes to you. You throw it the meat and it gives you the"
			print "\tkey to the next room because it is so intelligent, but"
			print "\tcan not feed itself."
			return Basement()
		else:
			return Forward("Do not COMPUTE!", Monster_room())
		
	
class Basement(object):
	"""You go down the stairs and into the basement.
	The basement has another door. The door has
	a keypad. You need to hack the massive 1 digit code.
	"""
	
	def run(self):
		code = "%d" % (randint(1,5))
		guesses = 0
		guess = raw_input("[Keypad]: ")
	
		while guess != code and guesses < 4:
			print "Try again:"
			guesses += 1
			guess = raw_input("[Keypad]: ")
	
		if guess == code:
			print "\tYay, good for you. You will not die of boredom."
			print "\tRight here right now anyway."
			print "\tYou see a misterious glow from behind another door"
			print "\tSice you got big cojones you open the door and enter."
			return Gold_room()
		else:
			print "\tWhether you do not know your numbers or just suck"
			print "\tYou can not crack the code and die of hunger."
			return death()
		
		
class Gold_room(object):
	"""You enter a room that is made entirely out of gold.
	You have to go over a suspicious looking bridge in orde
	to get out of the warehouse. There is some gold on floor.
	How many bars do you take?
	"""
	
	def run(self):
		gold = int(raw_input("# of bars: "))
	
		if gold < 10:
			print "\tCongratz, you win! You got out of the warehouse."
			exit(0)
		else:
			print "\tYou greedy mofo!"
			print "\tThe bridge collapses beacause you took to much!"
			print "\tEnjoy beeing dead."
			return death()
		

class Forward(Room):
	def __init__(self, message, next_room = None):
		Room.__init__(self)
		self.message = message
		self.next_room = next_room

	def show(self):
		print "\t" + self.message

	def run(self):
		return self.next_room
		

		
class death(Forward):
	def __init__(self, next_room):
		Forward.__init__(self, "You are dead.")


# why is this a class?!?
# class runner(object):

def play(next_room = Hallway()):
	while next_room:
		print "---------------" * 4
		next_room.show()
		next_room = next_room.run()

play()



Note that Forward is a generic class that can reduce the number of classes you have. You could replace Empty_room with it. Indeed, all these class instances seem messy. You could just get by with functions at this point.

e.g.
def forward(msg, nextRoom = None):
	print msg
	return nextRoom


def empty(room):
	return forward("""The room is totally empty.
		There is a crayon on the floor.
		Draw yourself a door to get out
		of this place.
		\tThat is one ugly ass door.
		""", room)


def death():
	return forward("You are dead.")

def hallway():
	print """You are in a warehouse with 2 doors.
	Which one do you try to open?
	Number 1 or 2?
	"""

	door = raw_input("> ")
	if door == "first" or door == "1": 
		return puzzle
	if door == "second" or door == 2:
		return death
	print "Do not COMPUTE!"
	return none


def monster():
	print """You have entered a dark room. In the center of the room
	is a fire. Above the fire is a sign. The sign says "Feed
	the beast". What do you do?
	"""
	
	the_beast = raw_input("I will: ")
	if "run" in the_beast:
		print "\tYou chicken! You run around for hours and die of dehydration."
		return death
	elif "wait" in the_beast:
		print "\tYou wait for 3 hours and die of boredom"
		print "\tNice job!"
		return death
	elif "feed" in the_beast:
		print "\tYou are brave, young badavan!"
		print "\tYou take the meat from the feed box and wave it around"
		print "\tFinally a little chihaua or smt that looks like a rat"
		print "\tcomes to you. You throw it the meat and it gives you the"
		print "\tkey to the next room because it is so intelligent, but"
		print "\tcan not feed itself."
		return basement
	else:
		print "Do not COMPUTE!"
		return monster


def puzzle():
	print """Puzzles, puzzles, puzzles, oh the lovely puzzles.
	I hope you like them. Here is one:
	Which of the three boxes in front of you has the key to
	the next room? And which one has a poisonous snake in it
	and which one has a grenade in it? Choose wisely:
	"""
	box = raw_input("Box #: ")

	if box == "1" or box == "one":
		print "\tYou chose the right box! Congratz!"
		print "\tGo explore some more!"
		return empty(monster)
	elif box == "2" or box == "two":
		print "\tOh nooo, the grenades splint is removed..."
		print "\tand you got blow into pieces. That is sad!"
		print "\tGame over!"
		return death
	elif box == "3" or box == "three":
		print "\tYaaay, the snake is sleeping and you try to open another box"
		print "\tAs soon as you move the snake wakes up and screams:"
		print "\tHello, muthafucka! And then he bites you. After which you"
		print "\ttwist and turn in agonising pain for 10 minutes and die"
		print "\tNice JOB!"
		return death
	else:
		print "Do not COMPUTE!"
		return none


	
def basement():
	print """You go down the stairs and into the basement.
	The basement has another door. The door has
	a keypad. You need to hack the massive 1 digit code.
	"""
	
	code = "%d" % (randint(1,5))
	guesses = 0
	guess = raw_input("[Keypad]: ")

	while guess != code and guesses < 4:
		print "Try again:"
		guesses += 1
		guess = raw_input("[Keypad]: ")

	if guess == code:
		print "\tYay, good for you. You will not die of boredom."
		print "\tRight here right now anyway."
		print "\tYou see a misterious glow from behind another door"
		print "\tSice you got big cojones you open the door and enter."
		return gold_room
	else:
		print "\tWhether you do not know your numbers or just suck"
		print "\tYou can not crack the code and die of hunger."
		return death
		
def gold_room():
	print """You enter a room that is made entirely out of gold.
	You have to go over a suspicious looking bridge in orde
	to get out of the warehouse. There is some gold on floor.
	How many bars do you take?
	"""
	
	gold = int(raw_input("# of bars: "))
	if gold < 10:
		print "\tCongratz, you win! You got out of the warehouse."
		return None
	else:
		print "\tYou greedy mofo!"
		print "\tThe bridge collapses beacause you took to much!"
		print "\tEnjoy beeing dead."
		return death
		

def play(next_room = hallway):
	while next_room:
		print "---------------" * 4
		next_room = next_room()



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 NoRemorse  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 11

Re: Class name / Import problem

Posted 18 January 2013 - 03:53 AM

Thank you, it does help. I used classes because this is an assignment from the book Learn Python The Hard Way and it told me to use classes :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1