Dumb guy.help me to solve ex43.py

hiiii.everyone says they learn pyhton too easily.i am not able to understand ex43.py.am i too dumb to learn programming.please help me.i am depressed.evryone posts and create new code.i am not able to.just map and engine

1 Like

Hello @waste_fellow.

If you have managed to get to exercise 43 you must have learned something.
I also think it is hard to get some parts of python into my head.
But you have to practice practice and practice and do not give up.
DO NOT SKIP THE STUDY DRILL!
If Zed says you should do 5 study drill. Do 10!
It will pay off later.

Put it in her like this (except for the ” )

”[code]

def your_function_here():
print(”something”)

[/code]

Then we can help with your code

@waste_fellow stop beating up yourself and don’t give yourself disrespectfull names. Give yourself some love and patience. Have your goal and keep going step by step every day a little bit.
Here is an interessting post about NO zero days:

If you are interested in something it means you have the ability to do it. Heads up!

@waste_fellow, this is an exercise that most people struggle with at first. Up until that point in the book, I was able to breeze through every exercise without much trouble. This is the exercise I first posted about on the forum. You should look take a look at the post I made; it’ll help to clarify some things.

From here on out in the book, be prepared to spend significantly more time on each exercise. Once I overcame Exercise 43 and tempered my expectations, I was able to get back on track.

Hey @waste_fellow, I see a lot of people are giving you encouragement, so let me ask you some questions:

  1. How long have you been trying to solve Ex43?
  2. Have you copied the Map and Engine code exactly and tried to get it working? Or, are you just reading it and thinking you should understand it?
  3. Have you written down the steps the code takes to do what it does?
  4. Have you printed out each argument and return value to study what is being sent to and from each object?

Finally, I have a new trick I’m telling people to use. Put this at the top of your file:

import pdb

That’s the Python Debugger, but really it just gives you the python prompt in the middle of your code. Now put this right before the part you don’t understand:

pdb.set_trace()

Then, when you run the program it will stop right at that point and you can play with the code. You can read the docs about PDB here https://docs.python.org/3.7/library/pdb.html

Give me the answers to your questions and I’ll help some more.

1 Like

i tried like for 3 days to learn about just oops trying small programmes.i know that i did not understand concept.then i finally got confident on concept and then tried this but could’nt understand just maps and engine and how its connected.i am not getting idea at all.

@waste_fellow That’s the way it is. It was same with me. But thats the point. You have to test your assumptions on real code and then adjust your assumptions. That’s how you grow.

I for myself had about two weeks or more till I really understand engine and map.
Here is what I did:

  1. draw a map:
  2. make comments on the code and print the variables, something like this:
class Scene(object): pass

class CentralCorridor(Scene):
    def enter(self): 
        input("Central Corridor. >")
        return 'finished'

class Finished(Scene):
    def enter(self): 
        print("You won! Good job.")
        return 'finished'

class Engine(object):
    # make an instance attribute named 'scene_map'
    # for scene map you have to set an instance of class Map() with a scene subclass as argument
    # e.g. Map('central_corridor')
    def __init__(self, scene_map):
        self.scene_map = scene_map
        print("Line 21: ", repr(self.scene_map))

    # make a method named 'play'
    def play(self):
        # set 'current_scene' to the following: from class Map call the method/function 'opening_scene'
        current_scene = self.scene_map.opening_scene()
        print("Line 26: ", repr(current_scene))
        # set 'last_scene' to the following: from class Map call the method/function
        # 'next_scene' with argument 'finished'
        last_scene = self.scene_map.next_scene('finished')
        print("Line 30: ", repr(last_scene))

        # run this as long as current_scene aren't the same
        while current_scene != last_scene:
            # set 'next_scene_name' to the following: call the method 'opening_scene' from
            # class Map. Method 'opening_scene' calls from the current scene (class) the method 'enter'
            # the return value of the 'enter' method is a scene name.
            next_scene_name = current_scene.enter()
            print("Line 38: ", repr(next_scene_name))
            # set 'current_scene' to the following: from class Map call the method 'next_scene' with
            # the return value of the 'enter' method of a scene subclass.
            current_scene = self.scene_map.next_scene(next_scene_name)
            print("Line 42: ", repr(current_scene))

        # run after the while loop is over
        # from 'current_scene' call the method 'enter'
        # program finishes
        current_scene.enter()
        print("Line 48: ", repr(current_scene.enter()))


class Map(object):
    # make a dictionary as a class attribute with the scene subclasses as value
    scenes = {
            'central_corridor': CentralCorridor(),
            'finished': Finished(),
    }
    
    # make an instance attribute named 'start_scene'
    # set as parameter a key-name from the class attribute dictionary scenes
    def __init__(self, start_scene):
        self.start_scene = start_scene

    # make a method called 'next_scene' with a parameter 'scene_name'
    def next_scene(self, scene_name):
        # set 'val' to the following: from class Map get from class attribute dictionary
        # 'scenes' the key speficied in the parameter of the function
        val = Map.scenes.get(scene_name)
        print("Line 68: ", repr(val))
        # return the scene subclass called in 'val'
        return val

    # make a method called 'opening_scene' with parameter 'self'
    def opening_scene(self):
        # return the method next_scene from map with parameter 'start_scene'
        print("Line 75: ", repr(self.next_scene(self.start_scene)))
        return self.next_scene(self.start_scene)


# set 'a_map' to an instance of class Map with the instance attribute 'central_corridor'
a_map = Map('central_corridor')
print("Line 81: ", repr(a_map))
# set 'a_game' to an instance of class Engine with class Map with the instance attribute 'central_corridor' as instance attribute
a_game = Engine(a_map)
print("Line 84: ", repr(a_game))
# from 'a_game' call the method play()
# or
# from class Engine call the method play()
a_game.play()
print("Line 89: ", repr(a_game.play()))

  1. write your own engine and map. I did it with two separate concepts (I can’t find my code, I have to look it up at home and will post it when).

  2. Try the advice from @zedshaw in his post above.

  3. You have to try and try. If you don’t get it after a while, try to implement the other code from the exercise and just copy zed’s engine and map. Then after you worked on the rest, you can go back and try again to understand it. So you have some progress, that will lift up your mood.

  4. Follow @zedshaw’s advice an write a E-Mail to yourself in which you describe the problem exactly.

  5. If you still have problems, post here the code-examples you don’t understand, etc. Be accurate and spend some time to really articulate the problem. So we can provide you with some help to your acctuall problem.

p.s.: @gpkesley has written a blogpost about the engine and map problem: Understanding the game engine

2 Likes

thank you so much.when i was reading next chapter inheritance vs composition,trying to learn something and finding more about that online. i got basic idea about code.with your help i understood fully.thank you for helping me.i am very much happy.

thank you sir.i finally understood concept.i know it sounds funny but bcz of that next chapter composition programme i understood this code.

@waste_fellow Awesome! Sometime it just needs some time and patience. I’m happy for you!

Hey @waste_fellow, looks like you’re getting it with @DidierCH help but here’s some more videos and code that might help you understand OOP:

https://blog.learncodethehardway.com/2018/08/15/free-short-course-for-understanding-object-oriented-programming-in-python/

In that I actually build an OOP system with Python code so you can understand what’s happening. Next thing, you can start trying to use this when there’s some code and you have no idea what’s happening at a spot:

https://docs.python.org/3/library/pdb.html

Put import pdb at the top of your file, then put pdb.set_trace() at the point you don’t understand, and then you can print out variables, run python code, and get insight into what’s going on.

1 Like

@zedshaw really like that pdb thing. It’s fantastic!

I should do a whole seminar on just pdb hacking.

That would be cool @zedshaw , but if you do, can you also include a bit on breakpoint() for 3.7 as this calls pdb as a builtin.

A seminar about pbd would be great.

What?! They added a breakpoint() in 3.7? About time.

1 Like

I have a much smaller question on this exercise. For the Finished class:


class Finished(Scene):

    def enter(self):
        print("You won! Good job.")
        return 'finished'

Is there any benefit to having it return ‘finished’ instead of just writing an exit(0) or having nothing at all after the print command? As far as I can tell, it doesn’t matter as once this class is called the engine kicks out of the while-loop.

class Engine(object):
    def __init__(self, scene_map):
        self.scene_map = scene_map

    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()

When I run the script with the Finished class ending in exit(0), return ‘finished’, or even with nothing at all, it performs the same.

I don’t think it matters. In terms of a finite state machine Finished is the final state so it’s just fine to exit.

In a more complex situation you might need to do some cleanup in the engine before shutting down, like closing files etc. Then you’d return.

@florian is right, not much of a reason other than if you plan on using this in say, a web application, then calling exit() is crazy bad. But if you never plan on doing that then don’t bother. Just exit.