Mercurial > python-cmd2
annotate docs/pycon2010/pirate6.py @ 402:be18c88a0fc8
report when end of transcript never seen
author | catherine.devlin@gmail.com |
---|---|
date | Sun, 07 Nov 2010 06:48:15 -0500 |
parents | 32b9137577b8 |
children |
rev | line source |
---|---|
376
32b9137577b8
avoiding 'str doesn't support the buffer api' exception
cat@eee
parents:
374
diff
changeset
|
1 from cmd2 import Cmd |
337 | 2 # prompts and defaults |
3 | |
4 class Pirate(Cmd): | |
5 gold = 3 | |
6 prompt = 'arrr> ' | |
7 def default(self, line): | |
374 | 8 print('What mean ye by "{0}"?' |
9 .format(line)) | |
337 | 10 def do_loot(self, arg): |
351 | 11 'Seize booty from a passing ship.' |
337 | 12 self.gold += 1 |
13 def do_drink(self, arg): | |
14 '''Drown your sorrrows in rrrum. | |
15 | |
16 drink [n] - drink [n] barrel[s] o' rum.''' | |
17 try: | |
18 self.gold -= int(arg) | |
19 except: | |
20 if arg: | |
338 | 21 print('''What's "{0}"? I'll take rrrum.'''.format(arg)) |
337 | 22 self.gold -= 1 |
351 | 23 def precmd(self, line): |
24 self.initial_gold = self.gold | |
25 return line | |
26 def postcmd(self, stop, line): | |
27 if self.gold != self.initial_gold: | |
374 | 28 print('Now we gots {0} doubloons' |
29 .format(self.gold)) | |
337 | 30 if self.gold < 0: |
374 | 31 print("Off to debtorrr's prison.") |
32 stop = True | |
337 | 33 return stop |
34 def do_quit(self, arg): | |
338 | 35 print("Quiterrr!") |
337 | 36 return True |
37 | |
38 pirate = Pirate() | |
372 | 39 pirate.cmdloop() |