-1

I am working out of the "Learn Python the Hard Way" book right now and I do not understand something. In one of the excersises the author uses some code I do not understand involving .enter()

def play(self):
    current_scene = self.scene_map.opening_scene()
    last_scene = self.scene_map.next_scene(’finished’)

    while current_scene != last_scene:
        next_scene_name = current_scene.enter()
        current_scene = self.scene_map.next_scene(next_scene_name)

    current_scene.enter()

I still don't totally understand classes, but I mostly understand what is going on here, save for enter. I can not find any documentation on it, and any information on why it is being used and why it is there would be very helpful. Thank you so much.

I am working out of exercises 43 in LPTHW from the newest edition. The specific part of the exercises is called Gothons from Planet Percal #25

4 Answers4

1

enter() must be a function of the scene class (which is generated in the first function line). It is not an in-built Python functionality

  • That is completely right, I feel so stupid now. I was very much overthinking it. Thank you so much and very sorry for the bad question. – Magical Man Jan 05 '17 at 01:20
0

It's hard to be precise without looking at the full exercise, but I would imagine that enter() is simply a method of a class called Scene and current_scene is an instance of that class.

airstrike
  • 2,270
  • 1
  • 25
  • 26
  • Also, from the text I saw online ,these seems like a terrible, terrible way to structure a game like that... – airstrike Jan 05 '17 at 01:22
0

I've taught from LPTHW a few times. The earlier answers are correct in the general solution. enter is a method of each class that you have to design; this is why there's no documentation on it. The purpose of this method is to do whatever you need to do when the player enters this room. Typical implementations include:

  • Print a welcome message
  • Print a description of the room (what does the player "see"?)
  • initialize other objects in the room, such as
    • Create a bear object
    • Determine the quantity of gold to be found
    • Update some game status, such as how many times the player has entered this room.

You're creating the world; the enter method is a significant part of presenting it to the player.

Have fun.

Prune
  • 76,765
  • 14
  • 60
  • 81
-1

Firstly, you can use pdb to debug your python application, just as below.

python -m pdb your.py

In the pdb, you can use the command as below to find some information.

help(current_scene.enter)

lxyscls
  • 297
  • 1
  • 3
  • 17
  • The poster is not trying to debug existing code; he's trying to figure out the purpose of the code to be used or written. – Prune Jan 05 '17 at 01:30
  • No no no. pdb is not only a debug tool. You can use it to comprehend the code. "Don't know the **enter** method"? Okay, "step in" in the pdb. – lxyscls Jan 05 '17 at 01:55
  • I'm not following: how does stepping into an empty routine help solve OPs problem? – Prune Jan 05 '17 at 02:04