[Tutor] exercise with classes
Prasad, Ramit
ramit.prasad at jpmorgan.com
Thu Feb 2 21:32:38 CET 2012
>I am learning Python using the "Learn Python the Hard Way" book by Zed Shaw. I reached exercise 42 where we learn about Python classes. The exercise shows a game with one class that includes all the definitions for playing the game. For extra credit we are asked to create another version of this game where we use two classes - one for the room definitions and the other for the playing the game. I feel stumped and don't know how to go about creating this game with two classes.
> So far I have searched for info on how to pass variables from one class to another and have been able to create a small two class program (attached). But I seem unable to generalize from here and apply this to the game exercise. What would you suggest for me to try next?
It is usually better to include short code directly in your email
or link to something like pastebin for longer samples. Many of the people
on this list do access it from email and they do not get attachments.
As for the extra credit: You can store the possible actions
in the room (dodge, shoot, etc.). So for central_corridor (an instance
of class Room) you have the player do something like
action = raw_input('>)
consequence = central_corridor.do_action( action )
new_room = Game.getRoom( consequence )
# Might need to replace Game with self depending on where this code is located.
action = raw_input('>)
consequence = new_room.do_action( action )
...
Ramit
Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423
--
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
More information about the Tutor
mailing list