12 Replies - 1811 Views - Last Post: 10 December 2015 - 02:57 PM Rate Topic: -----

#1 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 421
  • Joined: 15-December 14

Stuck on lesson #43 learn python the hard way

Posted 13 November 2015 - 12:14 AM

I'm trying to make the game Zed Shaw wants me to make in lesson #43 ("Gothons from Planet Percal #25") and I am stuck.In the lesson he wants me to make it similar to Zork or Adventure type game. I want to make the game interactive without having the player having to key in anything aside from the choices to play the game. I can get as far as having the player key in their name but after words the program won't go to the "start_scene" function. I don't understand why it says I don't have it defined if in the start_scene function I have it defined.I know how to loop between functions when I'm not using oop but I can't figure out how to make it work using oop. Below is the error and below that is my program so far. This is the error message I keep getting. Any and all suggestions are appreciated.

Traceback (most recent call last):
File "C:\Python27\ex043Map.py", line 15, in <module>
p1=Map("","")
File "C:\Python27\ex043Map.py", line 8, in __init__
print"Hello {}. Select one of the options".format(main_Menu)
NameError: global name 'main_Menu' is not defined



class Map(object):
    mainMenu=['start game','quit']
    def __init__(self,start_scene,name):
        self.start_scene=start_scene
        self.name=name
        print"Welcome to my game please enter your name"
        name=(raw_input())
        print"Hello {}. Select one of the options".format(main_Menu)
    def start_scene(self,mainMenu):
        main_Menu=['start game','quit']
    
        


p1=Map("","")
p1.start_scene



Is This A Good Question/Topic? 0
  • +

Replies To: Stuck on lesson #43 learn python the hard way

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6492
  • View blog
  • Posts: 26,266
  • Joined: 12-December 12

Re: Stuck on lesson #43 learn python the hard way

Posted 13 November 2015 - 01:51 AM

There error is in line 8 because main_Menu is not the same as mainMenu.
Was This Post Helpful? 0
  • +
  • -

#3 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 701
  • View blog
  • Posts: 2,867
  • Joined: 13-June 14

Re: Stuck on lesson #43 learn python the hard way

Posted 13 November 2015 - 02:21 AM

You're also going to have the problem that when you instantiate objects of your Map class, you'll rebind the name start_scene to whatever value you pass to __init__(), meaning that your objects will no longer have a method called start_scene(). You should avoid using the same name for different things, as it's confusing (and in this case will likely lead to bugs).

Also, you probably want to review PEP 8 to see the recommended style guidelines for Python.
Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7055
  • View blog
  • Posts: 14,733
  • Joined: 16-October 07

Re: Stuck on lesson #43 learn python the hard way

Posted 13 November 2015 - 07:40 AM

So, I think I basically found your assignment: https://lpthw.readth...en/latest/ex41/

I have a few minor problems with the code ( globals, exit, etc ) but otherwise it seems fine. The most striking thing here is the wonderful lack of any "class" types. Because, honestly, trying to wedge what you're doing into an object is a little like shoes on a fish.

If you want to offer a menu to your user; write a menu function. Something like:
def showMenu(state, options):
    # state will contain player's name and any other data related to the game
    # options are the menu options to display
    chosen = None
    while not chosen:
    # your code here
    return chosen



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

#5 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 421
  • Joined: 15-December 14

Re: Stuck on lesson #43 learn python the hard way

Posted 27 November 2015 - 09:26 PM

Ok, I fixed my program and followed the advice of baavgai. I have a few questions on making the character and aliens interact but thats for later. Right now I can't figure out how to loop from one method to another inside my oop program. I know how to so it using only functions but I can't seem to fix the problem using oop. This is what I have so far:


import random
class Map(object):
    def __init__(self):
        pass

    def start(self):
        print"Let's play Gothons From Planet Percal #25"
        print"Enter your name "
        playername=raw_input()
        print"Hello {},".format(playername)
        print intro
        print
        print "You're in a large corridor with two doors\
on either side of corridor you\
 are currently inside"
        for i in range(len(choice1)):
            print (choice1[i])
            
        starts=raw_input()
        if starts=='1':
            armoury()
        elif starts=='2':
            bridge()
        elif starts=='3':
            engineroom()
        else:
            print"Select one of the options"
                

    def armoury(self):
        print"armoury"

    def bridge(self):
        print"bridge"

    def engineroom(self):
        print"engine room"
        


choice1=["what do you want to do?:",\
         "1. Go to the armoury and grab a weapon?",\
         "2. Go to the bridge to call for help?",\
         "3. Go to the engine room to get the self destruct code?"]
         
intro="Alien chickens have invaded a space ship led by the evil Col Sanders \
and our hero has to go through a maze of rooms defeating the so he can \
escape into an escape pod to the planet below "



p1=Map()
p1.start()
p1.bridge()
p1.armoury()
p1.engineroom()




Was This Post Helpful? 0
  • +
  • -

#6 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 701
  • View blog
  • Posts: 2,867
  • Joined: 13-June 14

Re: Stuck on lesson #43 learn python the hard way

Posted 27 November 2015 - 10:52 PM

View Postalbert003, on 28 November 2015 - 04:26 AM, said:

Right now I can't figure out how to loop from one method to another inside my oop program.


What do you mean by this?
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7055
  • View blog
  • Posts: 14,733
  • Joined: 16-October 07

Re: Stuck on lesson #43 learn python the hard way

Posted 28 November 2015 - 04:25 AM

View Postalbert003, on 28 November 2015 - 12:26 AM, said:

Ok, I fixed my program and followed the advice of baavgai.


In what way, exactly? You still have globals. You still have a senseless object with an unfortunate name. There is no menu function.

In the code I linked to, which looks like your assignment, the current room you were in was a function being pointed to. This is a good concept to know, though a little advanced for the rest of the program.

Given this, your code could be something like:
def menu(prompt, opts):
    # your code here

def intro():
    # your code here
    return roomMain

def roomMain():
    print "You're in a large corridor with two doors on either side of corridor you are currently inside"
    opts = [
        ("Go to the armoury and grab a weapon?", roomArmoury),
        ("Go to the bridge to call for help?", roomBridge),
        ("Go to the engine room to get the self destruct code?", roomEngine)
        ]
    return menu("what do you want to do?:", opts)
                
def roomArmoury():
    # your code here

def roomBridge():
    # your code here

def roomEngine():
    # your code here


def main():
    room = intro()
    while room:
        nextRoom = room()
        room = nextRoom
    print "done"

main()



If you insisted on a object instance, then something like:
class Game(object):
    def __init__(self):
        # if you can skip init, then you probably aren't doing OOP
        self.currentRoom = None
        self.playerName = None

    def menu(self, prompt, opts):
        # your code here

    def intro(self):
        # your code here
        self.currentRoom = self.roomMain

    def roomMain(self):
        print "You're in a large corridor with two doors on either side of corridor you are currently inside"
        opts = [
            ("Go to the armoury and grab a weapon?", roomArmoury),
            ("Go to the bridge to call for help?", roomBridge),
            ("Go to the engine room to get the self destruct code?", roomEngine)
            ]
        self.menu("what do you want to do?:", opts)
                    
    def roomArmoury(self):
        # your code here

    def roomBridge(self):
        # your code here

    def roomEngine(self):
        # your code here

    def run(self):
        self.intro()
        while self.currentRoom:
            self.currentRoom()
        print "done"

Game().run()



Good luck.
Was This Post Helpful? 0
  • +
  • -

#8 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 421
  • Joined: 15-December 14

Re: Stuck on lesson #43 learn python the hard way

Posted 09 December 2015 - 01:56 PM

Ok, I re read the lesson and took a peek at how he did it (But I didn't take notes so I could see how close I was)and I wasn't even close. I re did it using what both you suggested and what Zed Shaw suggested. I have a new question for you. When I run the program I get an error saying "class corridorRoom(menus):
NameError: name 'menus' is not defined". I don't understand how its not defined if I have it in the constructor.

class Engine:
    def __init__(self):
        self.menus = [corridorRoom(),engineRoom(),armouryRoom(),escapePod()]
        self.currentRoom=None
        self.playerName=None

    def menu(self,prompt,choice):
        self.currentRoom=corridorRoom
        self.prompt=prompt#used whith raw_input
        self.choice=choice#used with if,elif,else

class corridorRoom(menus):
    def enter(self):
        print "Hello {},".format(playername [0].upper()+playername[1:].lower())
        print "Aliens have invaded your space ship and our hero has to go through a maze"
        print "of rooms defeating them so he can escape into an"
        print "escape pod to the planet below."


    def run(self):
        self.corridorRoom()
        while self.currentRoom:
            self.currentRoom()
        
Engine().run()

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6492
  • View blog
  • Posts: 26,266
  • Joined: 12-December 12

Re: Stuck on lesson #43 learn python the hard way

Posted 09 December 2015 - 02:08 PM

Post the full error details, which also tell you/us what line the error occurs on. It might be line 12, but it is much better than you confirm the full error details.

Note that corridorRoom (classes should start with a capital letter, CorridorRoom) is a completely separate class from Engine. If you want it to be a nested class then it needs to be indented.
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 10881
  • View blog
  • Posts: 18,568
  • Joined: 19-March 11

Re: Stuck on lesson #43 learn python the hard way

Posted 09 December 2015 - 03:00 PM

class corridorRoom(menus):


This means you're trying to inherit from some class called menus, which does not exist in the code you're showing here. What is it you're trying to do with "menus" here?
Was This Post Helpful? 0
  • +
  • -

#11 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 421
  • Joined: 15-December 14

Re: Stuck on lesson #43 learn python the hard way

Posted 09 December 2015 - 03:10 PM

It is line 12. This is the error message I keep getting when I try and run the program. I also included the way Zed Shaw said he would make the program for us to use as a template below.
This is the error message.
line 12, in <module>
    class CorridorRoom(menus):
NameError: name 'menus' is not defined


This is the way he wrote his template and when I took a peek below he made each part of the game with a separate class.


class Scene(object):

    def enter(self):
        pass


class Engine(object):

    def __init__(self, scene_map):
        pass

    def play(self):
        pass

class Death(Scene):

    def enter(self):
        pass

class CentralCorridor(Scene):

    def enter(self):
        pass

class LaserWeaponArmory(Scene):

    def enter(self):
        pass

class TheBridge(Scene):

    def enter(self):
        pass

class EscapePod(Scene):

    def enter(self):
        pass


class Map(object):

    def __init__(self, start_scene):
        pass

    def next_scene(self, scene_name):
        pass

    def opening_scene(self):
        pass


a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()

Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 10881
  • View blog
  • Posts: 18,568
  • Joined: 19-March 11

Re: Stuck on lesson #43 learn python the hard way

Posted 09 December 2015 - 03:16 PM

class Scene(object):



Here, Scene inherits from object. That is, it's just a class.

You could then do something like

class SpecialScene(Scene):


and then you'd be defining a class which specialized Scene for some special purpose - inheritance.

This is different from

def my_func(menus):


where menus is a parameter to the function. The difference is that in the latter example, you're declaring a variable, which will get its value when the function is called. In the former case, you're passing an object to a function, and that object must a, be a class and b, exist.
Was This Post Helpful? 0
  • +
  • -

#13 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 421
  • Joined: 15-December 14

Re: Stuck on lesson #43 learn python the hard way

Posted 10 December 2015 - 02:57 PM

I read your answer and the following lesson he discusses inheritance. I had to look online to see how that worked and to get a clearer picture of what was going on in the program. I realise what mistake I made when I made the original program I wrote along with your help and taking a peek at how he made his final program helped a lot. I do have a new question for you.... I get how oop works, but I still don't get how programmers make the whole thing 'run' when they run it using oop. I've seen different ways people have done it but I can't find an explanation of why they did it that way or why they chose that particular way to do it. I re did my program and it works, I haven't been able to make the last method with 'run' work. Can you please tell me how and why it is done that way? This is my program after your advice.

import random
from time import sleep
from sys import stdout

class Engine(object):
    def __init__(self):
        self.PlayerName=None
        self.CurrentRoom=None

    def stuff(self):#Misc stuff needed for the game
        self.PlayerName=PlayerName#Used to input the players choice
        self.opts = {"Corridor": Corridor(),"ArmouryRoom": ArmouryRoom(),"Bridge": Bridge(),
                     "EscapePod": EscapePod(),"Death": death()
                     }
        self.CurrentRoom=Main

class Main(Engine):
    def Enter(self):
        for i in range(len(intro)):
            stdout.write(intro[i])
            stdout.flush()
            sleep(.1)
        print
        PlayerName=raw_input()
        print"Hello {}".format(PlayerName [0].upper()+PlayerName[1:].lower())
        for i in range(len(a)):
            stdout.write(a[i])
            stdout.flush()
            sleep(.1)
        print
        opts=raw_input()
        if opts =="1":
            ArmouryRoom()
        elif opts =="2":
            EngineRoom()
        else:
            print"Select one of the options"
            return a

class ArmouryRoom(Engine):
    def Enter(self):
        print "armoury room"

  
        
intro="""Alien chickens have invaded your space ship led by the evil Col Sanders
and our hero has to go through a maze of rooms defeating them so he can
escape into an escape pod to the planet below.
Let's play Gothons From Planet Percal #25
Enter your name """
a="""You're in a large corridor with two doors on either side of corridor
you are currently inside
what do you want to do?:
1. Go to the armoury to grab a weapon?
2. Go to the engine room to get the self destruct code?
"""


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1