Question about files?
I want to create a program where a user can type what ever they want to, have it saved to a file, and the be able to re-open it and read it. How would I do this? Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: Question about files?
On May 31, 3:25 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On Sat, 31 May 2008 11:44:14 -0700 (PDT), [EMAIL PROTECTED] > declaimed the following in comp.lang.python: > > > I want to create a program where a user can type what ever they want > > to, have it saved to a file, and the be able to re-open it and read > > it. How would I do this? Thanks! > > import os > os.system("edit") # may be Windows specific > > Start with:http://www.catb.org/~esr/faqs/smart-questions.html > > (or, to expand on it... Exactly what part of the task are you having > problems with? Opening files for read/write/update? Designing a command > set for an editor? Designing a GUI for a text editor; what toolkit, does > it have a generic text edit widget already? Do you really need to write > another text editor when there are so many available that come with most > operating systems (Windows: edit, notepad, maybe even wordpad; > UNIX/Linux type systems: vi, vim, gvim, emacs) or can be downloaded > (SciTE) ) > > -- > Wulfraed Dennis Lee Bieber KD6MOG > [EMAIL PROTECTED] [EMAIL PROTECTED] > HTTP://wlfraed.home.netcom.com/ > (Bestiaria Support Staff: [EMAIL PROTECTED]) > HTTP://www.bestiaria.com/ No, what I mean is for the user to input text directly into the python program, then the program would save it to a file and display it after it is created. -- http://mail.python.org/mailman/listinfo/python-list
text adventure game problem
okay, I'm having this one problem with a text adventure game. It's kind of hard to explain, but I'll do my best. [code] def prompt_kitchen(): global gold gold_taken = False while True: prompt_kit = raw_input('>') if prompt_kit == 'examine cabinet 1' and not gold_taken: print '''This cabinet has a lot of cups in it with all different designs and shapes. Where are the people anyway? How come there's nobody here? In one of the cups you find 8 gold.''' gold = gold+8 gold_taken = True pass4() elif prompt_kit == 'examine cabinet 1' and gold_taken: print \ '''This cabinet has a lot of cups in it with all different designs and shapes. Where are the people anyway? How come there's nobody here?''' pass4() def pass4(): global gold print 'You have', gold, 'gold' pass [/code] Okay, now for my problem. In the above function, there's the option to examine a cabinet and get 8 gold. (everyone here knows that...but I'm just trying to state my problem...) Unfortunately, it kind of doesn't work. After the first time I 'examine cabinet 1' in my game, I get 8 gold and I can't get it again. But, If I leave the room and come back to it, then it's as if I had never gotten the gold the first time, and I can get it again. How do I fix this? -- http://mail.python.org/mailman/listinfo/python-list
Re: text adventure game problem
On Apr 8, 9:25 pm, André <[EMAIL PROTECTED]> wrote: > On Apr 8, 10:01 pm, [EMAIL PROTECTED] wrote: > > > > > okay, I'm having this one problem with a text adventure game. It's > > kind of hard to explain, but I'll do my best. > > [code] > > > def prompt_kitchen(): > > global gold > > gold_taken = False > > while True: > > prompt_kit = raw_input('>') > > if prompt_kit == 'examine cabinet 1' and not gold_taken: > > print '''This cabinet has a lot of cups in it with all > > different > > designs and shapes. Where are the people anyway? How come there's > > nobody here? > > In one of the cups you find 8 gold.''' > > gold = gold+8 > > gold_taken = True > > pass4() > > elif prompt_kit == 'examine cabinet 1' and gold_taken: > > print \ > > '''This cabinet has a lot of cups in it with all > > different > > designs and shapes. Where are the people anyway? How come there's > > nobody here?''' > > pass4() > > > def pass4(): > > global gold > > print 'You have', gold, 'gold' > > pass > > [/code] > > > Okay, now for my problem. > > In the above function, there's the option to examine a cabinet and get > > 8 gold. (everyone here knows that...but I'm just trying to state my > > problem...) > > Unfortunately, it kind of doesn't work. > > After the first time I 'examine cabinet 1' in my game, I get 8 gold > > and I can't get it again. > > But, If I leave the room and come back to it, then it's as if I had > > never gotten the gold the first time, and I can get it again. > > How do I fix this? > > quick guess: define gold_taken as a global variable and initialize it > outside of the function. > > Warning: avoid global variables if at all possible. > > ;-) > André Here's a sample code that, in fact, does work. In this code, when run, I can only get the gold once. def prompt_house(): global gold gold_taken = False while True: prompt_hou = raw_input('>') if prompt_hou == 'examine table' and not gold_taken: print \ '''There are a lot of car magazines here. You flip through them and find 5 gold. ''' gold = gold+5 gold_taken = True elif prompt_hou == 'go west': # this gets you out of the loop go_west() # more elif choices here ... elif prompt_hou == 'examine table' and gold_taken: print '''There are a lot of car magazines here.''' go_west() def go_west(): # just a dummy funk global gold print gold pass # test gold = 0 prompt_house() But what's the difference between this and the one that I posted? -- http://mail.python.org/mailman/listinfo/python-list
Re: text adventure game problem
On Apr 8, 9:55 pm, André <[EMAIL PROTECTED]> wrote: > On Apr 8, 10:44 pm, [EMAIL PROTECTED] wrote: > > > > > On Apr 8, 9:25 pm, André <[EMAIL PROTECTED]> wrote: > > > > On Apr 8, 10:01 pm, [EMAIL PROTECTED] wrote: > > > > > okay, I'm having this one problem with a text adventure game. It's > > > > kind of hard to explain, but I'll do my best. > > > > [code] > > > > > def prompt_kitchen(): > > > > global gold > > > > gold_taken = False > > > > while True: > > > > prompt_kit = raw_input('>') > > > > if prompt_kit == 'examine cabinet 1' and not gold_taken: > > > > print '''This cabinet has a lot of cups in it with all > > > > different > > > > designs and shapes. Where are the people anyway? How come there's > > > > nobody here? > > > > In one of the cups you find 8 gold.''' > > > > gold = gold+8 > > > > gold_taken = True > > > > pass4() > > > > elif prompt_kit == 'examine cabinet 1' and gold_taken: > > > > print \ > > > > '''This cabinet has a lot of cups in it with all > > > > different > > > > designs and shapes. Where are the people anyway? How come there's > > > > nobody here?''' > > > > pass4() > > > > > def pass4(): > > > > global gold > > > > print 'You have', gold, 'gold' > > > > pass > > > > [/code] > > > > > Okay, now for my problem. > > > > In the above function, there's the option to examine a cabinet and get > > > > 8 gold. (everyone here knows that...but I'm just trying to state my > > > > problem...) > > > > Unfortunately, it kind of doesn't work. > > > > After the first time I 'examine cabinet 1' in my game, I get 8 gold > > > > and I can't get it again. > > > > But, If I leave the room and come back to it, then it's as if I had > > > > never gotten the gold the first time, and I can get it again. > > > > How do I fix this? > > > > quick guess: define gold_taken as a global variable and initialize it > > > outside of the function. > > > > Warning: avoid global variables if at all possible. > > > > ;-) > > > André > > > Here's a sample code that, in fact, does work. In this code, when run, > > I can only get the gold once. > > > def prompt_house(): > > global gold > > gold_taken = False > > while True: > > prompt_hou = raw_input('>') > > if prompt_hou == 'examine table' and not gold_taken: > > print \ > > '''There are a lot of car magazines here. > > You flip through them and find 5 gold. > > ''' > > gold = gold+5 > > gold_taken = True > > elif prompt_hou == 'go west': > > # this gets you out of the loop > > The above comment is wrong. > > > go_west() > > # more elif choices here ... > > elif prompt_hou == 'examine table' and gold_taken: > > print '''There are a lot of car magazines here.''' > > go_west() > > def go_west(): > > # just a dummy funk > > global gold > > print gold > > pass > > The "pass" statement is redundant. > > > # test > > gold = 0 > > prompt_house() > > > But what's the difference between this and the one that I posted? > > It is hard to say as you are not posting the entire code. As I > indicated above, you wrote a comment indicating that a given choice > was taking you out of the loop - which could only happen through a > break statement. You may want to post a ("small") code sample that > can be run by itself and reproduces the problem behaviour you > observe. The sample you posted include infinite loops with no way to > get out, so your original claim that you could leave the room and come > back is highly suspicious ;-) > > André Here ya go...this is an excerpt from my main code, with an example room added on. gold = 0 def kitchen(): print 'you have', gold, 'gold' print '''You are in the kitchen of the house. There is a lot of cooking equipment here, along with 3 cabinets, a food pantry, and a drawer. At the far end of the room is an icebox and a stove. To the south there is a living room, and to the east is a den.''' print prompt_kitchen() def prompt_kitchen(): global gold gold_taken = False while True: prompt_kit = raw_input('>') if prompt_kit == 'examine cabinet 1' and not gold_taken: print '''This cabinet has a lot of cups in it with all different designs and shapes. Where are the people anyway? How come there's nobody here? In one of the cups you find 8 gold.''' gold = gold+8 gold_taken = True pass4() elif prompt_kit == 'examine cabinet 1' and gold_taken: print \ '''This cabinet has a lot of cups in it with all different designs and shapes. Where are the people anyway? How come there's nobody here?''' pass4() elif prompt_kit == 'south': extra_room() def extra_room(): print 'you
Re: text adventure game problem
On Apr 9, 1:24 am, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On Tue, 8 Apr 2008 18:01:01 -0700 (PDT), [EMAIL PROTECTED] > declaimed the following in comp.lang.python: > > > okay, I'm having this one problem with a text adventure game. It's > > kind of hard to explain, but I'll do my best. > > [code] > > > def prompt_kitchen(): > > global gold > > gold_taken = False > > while True: > > prompt_kit = raw_input('>') > > if prompt_kit == 'examine cabinet 1' and not gold_taken: > > print '''This cabinet has a lot of cups in it with all > > different > > designs and shapes. Where are the people anyway? How come there's > > nobody here? > > In one of the cups you find 8 gold.''' > > Forgive me, but Ugh! > > It looks like you are defining a function for each possible room > which incorporates code for the actions possible in that room... > > I suspect I'd try to create a generic room class which has as > attributes a dictionary of actions and a dictionary of movable objects. > Along with methods that work on objects... Working an action that digs > into other objects may take some doing. > > class gobject(object): > def examine(self, item=None): > if not item: item = self > print "you look at the %s; you see %s" % \ > (item.desc, > ", ".join([k for k in > item.subobjs.keys()])) > def go(self, direction): > return moves[directions] > def take(self, item): > itmobj = self.subobjs[item] > del self.subobjs[item] > return itmobj > def dropto(self, itemname, item): > self.subobjs[itemname] = item > > class movobj(gobject): > def __init__(self, desc="a vague blob", subobjs=None, moves=None): > ... > > class room(gobject): > def __init__(self, desc="a bare chamber", > subobjs=None, moves=None): > ... > > g1 = movobj(desc="8 pieces of gold") > c1 = movobj(desc="kitchen cabinet", subobjs={"gold" : g1}) > > rooms = {"kichen" : room(desc="you are in the kitchen", > subobjs={"cabinet" : > c1}, > moves={"n" : > rooms["dining"], > "out" > : rooms["dining"} > "dining" : room(desc="you are in the dining room", > moves={"s" : > rooms["kitchen"} } > > player = Player() > player.location = rooms["dining"] > player.run() > > {where run() incorporates the parser loop -- something like} > while True: > self.location.examine() > command = raw_input("=> ") > words = command.split() > if words[0] in ["go", "move", "walk", "run"]: > self.location = self.location.go(words[1]) > elif words[0] in ["look", "examine", "study"]: > self.location.examine(words[1]) > elif words[0] in ["take"]: > self.subobjs[words[1]] = self.location.take(words[1]) > elif words[0] in ["drop", "leave"]: > self.location.dropto(words[1], self.subobjs[words[1]]) > del self.subobjs[words[1]] > elif ... > > {of course, this ignores adding logic for failure to find the > subobj/move in the relevant dictionary -- which should result in > > I do not see that here > or > I can not go that direction > > objects should also have attributes to indicate if they can be thrown > (and what the results are), etc. > > In the above the display should look something like (ignoring the > unchecked for missing attributes) > > you look at the dining room, you see ? (need check for empty) > => > > go s > > you look at the kitchen, you see cabinet > => > > examine cabinet > > you look at the kitchen cabinet, you see gold > => > > -- > Wulfraed Dennis Lee Bieber KD6MOG > [EMAIL PROTECTED] [EMAIL PROTECTED] > HTTP://wlfraed.home.netcom.com/ > (Bestiaria Support Staff: [EMAIL PROTECTED]) > HTTP://www.bestiaria.com/ I can't even compile your code to see how it works, Dennis. I'm confused about what that does. -- http://mail.python.org/mailman/listinfo/python-list
Re: text adventure game problem
On Apr 10, 3:14 am, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On Wed, 9 Apr 2008 05:25:19 -0700 (PDT), [EMAIL PROTECTED] > declaimed the following in comp.lang.python: > > > > > I can't even compile your code to see how it works, Dennis. I'm > > confused about what that does. > > My apologies -- it was spur of the moment pseudo-code to illustrate > the concepts, but was never meant to be directly executable. > > The concepts are that: all rooms are basically the same -- they have > a list of objects that exist within the room (and, as I now realize, the > "gold within the cabinet/cup" would still be an object within the room > -- but would have an attribute "hidden=True" which would control if it > is itemized when one "examines" the room), a list of exits, and a maybe > a list of actions which can be performed in the room or on named objects > (though I see the actions being common methods invoked by the command > parser -- I illustrated a simple "verb object" command, but a good > parser should probably accept more complex commands "throw at > " for example). Uh, I'm saying "list", but "dictionary" would > be the likely implementation. > > When one "examines" a room, one essentially gets back a list of the > objects that are in the room instance. When one "examines" one of those > objects (by name), one gets back the detailed description of that > object. If one implements a "hidden" attribute, that object will not be > listed in the higher level "examine". That "gold" inside another object > does get a bit tricky -- I now see it as a hidden object in the room, > but the description of the object has to be somewhat dynamic -- that is, > if the object (cabinet) sees that the "gold" is in the room, examining > the object will report the gold. But a "take gold" command still acts on > the room -- removing the gold from the room inventory, putting it into > the player inventory, and changing the "hidden" property so it is now > visible. > > When one does a "move ", the player's "location" > attribute is changed from the current room to the room connected to that > direction. > > For both objects and directions, if the player enters a term that is > not in the relevant dictionary, a suitable message is returned to the > user. Oh, room objects, besides that "hidden" attribute, may have a > "portable" attribute which controls if one can "take" the object... Or > an attribute for "throwable" which controls if one can throw an object > at another (and the other would have a method/action for when something > is thrown at it -- a default would be "No effect"). > > From what I saw of your small sample, you were treating each room as > a discrete function. Moving from one room to another means calling the > new room's function. But that means you are continuously nesting deeper > in the Python stack, and someone that keeps moving back and forth > between two rooms will eventually exceed the Python stack limit. > > By making the "current location" an attribute of a "player > instance", no recursion is involved -- you move between rooms by simply > assigning the new room as the "current location". > > -- > Wulfraed Dennis Lee Bieber KD6MOG > [EMAIL PROTECTED] [EMAIL PROTECTED] > HTTP://wlfraed.home.netcom.com/ > (Bestiaria Support Staff: [EMAIL PROTECTED]) > HTTP://www.bestiaria.com/ okay, that explains it... could you provide a working example of a two-room game using your method please so I can understand it better? Thanks in advance! -- http://mail.python.org/mailman/listinfo/python-list
Re: text adventure game problem
On Apr 11, 1:40 am, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On Thu, 10 Apr 2008 05:06:42 -0700 (PDT), [EMAIL PROTECTED] > declaimed the following in comp.lang.python: > > > okay, that explains it... > > could you provide a working example of a two-room game using your > > method please so I can understand it better? Thanks in advance! > > Okay... It isn't the best thought out system -- I have too > many specific classes... It would probably be better to put actions into > dictionaries and use some custom .getattr() to handle them. > > Will four rooms, a hidden chunk of gold, and one other movable > object suffice? Watch out for news client line wrapping. The only > parsing done is of the form: verb object; no attempt to handle: verb > direct_object filler indirect_object > > -=-=-=-=-=-=- > # > # TAGS.py Text Adventure Game Shell > # > > # I've probably defined too many special classes here > class Gobject(object): #Game object > def __init__(self, name, description, hidden=False, fixed=True): > self._name = name > self._description = description > self.hidden = hidden #hidden objects are not visible to > EXAMINE > self.fixed = fixed #fixed objects can not be taken > def _getName(self): > return self._name > name = property(_getName) > def _getDescription(self): > return self._description > description = property(_getDescription) > > class Exit(Gobject): > def __init__(self, description, target, hidden=False): > super(Exit, self).__init__(None, description, hidden) > self._target = target > def _getTarget(self): > return self._target > target = property(_getTarget) > > class Room(Gobject): #rooms (container object) > def __init__(self, name, description, exits=None, details=None): > super(Room, self).__init__(name, description, hidden=False, > fixed=True) > self._exits = exits > if details: > self._details = details #other objects that are inside > the room > else: > self._details = {} > def setExits(self, exits): > self._exits = exits > def go(self, ext): > return self._exits.get(ext, None) > def setDetails(self, details): > self._details = details > def addDetail(self, itm): > self._details[itm.name] = itm > def delDetail(self, name): > if (name in self._details and > not self._details[name].fixed): > itm = self._details[name] > del self._details[name] > else: > itm = None > return itm > def examine(self, name=None): > if not name: return self.description > if (name in self._details > and not self._details[name].hidden): > return self._details[name].description > else: > return None > def _detailedDescription(self): > items = "nothing of interest" > if self._details: > items = ", ".join([itm.name for itm in > self._details.values() > if not itm.hidden]) > exits = ", ".join([ext for ext in self._exits.keys() > if not self._exits[ext].hidden]) > #there must be at least one exit (the way you came in) > return "%s. In the %s you see %s. Passages lead to %s." % > (self._description, > self.name, > items, > exits) > description = property(_detailedDescription) > > class Thing(Gobject): > def __init__(self, name, description, parent, hidden=False, > fixed=False): > super(Thing, self).__init__(name, description, hidden, fixed) > self.parent = parent > def take(self): > if self.fixed: > return None > else: > self.hidden = False #if taken, one now can see it > return self.parent.delDetail(self.name) > def drop(self, parent): > self.parent.delDetail(self.name) > parent.addDetail(self) > > class TriggerThing(Thing): > def __init__(self, name, description, parent, > hidden=False, fixed=True, triggers=None): > super(TriggerThing, self).__init__(name, description, parent, > hidden, fixed) > self._triggers = triggers > def _detailedDescription(self): > if self._triggers: > items = ", ".join([itm.name for itm in > self.parent._details.values() > if itm in self._triggers > and itm.hidden]) > else: > items = ", ".join([itm.name for item in > self.parent._details.values() > if itm.hidden]) > if not items: items = "nothing of interest" > return "%s. In the %s yo
Re: text adventure game problem
On Apr 11, 10:16 am, [EMAIL PROTECTED] wrote: > On Apr 11, 1:40 am, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > > > On Thu, 10 Apr 2008 05:06:42 -0700 (PDT), [EMAIL PROTECTED] > > declaimed the following in comp.lang.python: > > > > okay, that explains it... > > > could you provide a working example of a two-room game using your > > > method please so I can understand it better? Thanks in advance! > > > Okay... It isn't the best thought out system -- I have too > > many specific classes... It would probably be better to put actions into > > dictionaries and use some custom .getattr() to handle them. > > > Will four rooms, a hidden chunk of gold, and one other movable > > object suffice? Watch out for news client line wrapping. The only > > parsing done is of the form: verb object; no attempt to handle: verb > > direct_object filler indirect_object > > > -=-=-=-=-=-=- > > # > > # TAGS.py Text Adventure Game Shell > > # > > > # I've probably defined too many special classes here > > class Gobject(object): #Game object > > def __init__(self, name, description, hidden=False, fixed=True): > > self._name = name > > self._description = description > > self.hidden = hidden #hidden objects are not visible to > > EXAMINE > > self.fixed = fixed #fixed objects can not be taken > > def _getName(self): > > return self._name > > name = property(_getName) > > def _getDescription(self): > > return self._description > > description = property(_getDescription) > > > class Exit(Gobject): > > def __init__(self, description, target, hidden=False): > > super(Exit, self).__init__(None, description, hidden) > > self._target = target > > def _getTarget(self): > > return self._target > > target = property(_getTarget) > > > class Room(Gobject): #rooms (container object) > > def __init__(self, name, description, exits=None, details=None): > > super(Room, self).__init__(name, description, hidden=False, > > fixed=True) > > self._exits = exits > > if details: > > self._details = details #other objects that are inside > > the room > > else: > > self._details = {} > > def setExits(self, exits): > > self._exits = exits > > def go(self, ext): > > return self._exits.get(ext, None) > > def setDetails(self, details): > > self._details = details > > def addDetail(self, itm): > > self._details[itm.name] = itm > > def delDetail(self, name): > > if (name in self._details and > > not self._details[name].fixed): > > itm = self._details[name] > > del self._details[name] > > else: > > itm = None > > return itm > > def examine(self, name=None): > > if not name: return self.description > > if (name in self._details > > and not self._details[name].hidden): > > return self._details[name].description > > else: > > return None > > def _detailedDescription(self): > > items = "nothing of interest" > > if self._details: > > items = ", ".join([itm.name for itm in > > self._details.values() > > if not itm.hidden]) > > exits = ", ".join([ext for ext in self._exits.keys() > > if not self._exits[ext].hidden]) > > #there must be at least one exit (the way you came in) > > return "%s. In the %s you see %s. Passages lead to %s." % > > (self._description, > > self.name, > > items, > > exits) > > description = property(_detailedDescription) > > > class Thing(Gobject): > > def __init__(self, name, description, parent, hidden=False, > > fixed=False): > > super(Thing, self).__init__(name, description, hidden, fixed) > > self.parent = parent > > def take(self): > > if self.fixed: > > return None > > else: > > self.hidden = False #if taken, one now can see it > > return self.parent.delDetail(self.name) > > def drop(self, parent): > > self.parent.delDetail(self.name) > > parent.addDetail(self) > > > class TriggerThing(Thing): > > def __init__(self, name, description, parent, > > hidden=False, fixed=True, triggers=None): > > super(TriggerThing, self).__init__(name, description, parent, > > hidden, fixed) > > self._triggers = triggers > > def _detailedDescription(self): > > if self._triggers: > > items = ", ".join([itm.name for itm in > > self.parent._details.values() > > if itm in self._triggers > >