Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #46896
| From | Roy Smith <roy@panix.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Beginner question |
| Date | 2013-06-04 09:16 -0400 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <roy-E4E37F.09162004062013@news.panix.com> (permalink) |
| References | <323f2f5b-1f50-4689-90b8-74c411e43971@googlegroups.com> <IKKdneVcX7wyMjDMnZ2dnUVZ_sSdnZ2d@giganews.com> |
In article <IKKdneVcX7wyMjDMnZ2dnUVZ_sSdnZ2d@giganews.com>,
Larry Hudson <orgnut@yahoo.com> wrote:
> def partdeux():
> print('A man lunges at you with a knife!')
> option = input('Do you DUCK or PARRY? ').lower()
> success = random.randint(0, 1)
> if success:
> if option == 'duck':
> print('He tumbles over you')
> return
> if option == 'parry':
> print('You trip him up')
> return
> print('He stabs you')
I'm going to suggest another possible way to organize this. I'm not
claiming it's necessarily better, but as this is a learning exercise,
it's worth exploring. Get rid of all the conditional logic and make
this purely table-driven:
responses = {(0, 'duck'): "He tumbles over you",
(0, 'parry'): "You trip him up",
(1, 'duck'): "He stabs you",
(1, 'parry'): "He stabs you",
}
and then....
def partdeux():
print('A man lunges at you with a knife!')
option = input('Do you DUCK or PARRY? ').lower()
success = random.randint(0, 1)
print responses[(success, option)]
Consider what happens when the game evolves to the point where you have
four options (DUCK, PARRY, RETREAT, FEINT), multiple levels of success,
and modifiers for which hand you and/or your opponent are holding your
weapons in? Trying to cover all that with nested logic will quickly
drive you insane.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Beginner question eschneider92@comcast.net - 2013-06-03 20:39 -0700
RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 09:46 +0300
Re: Beginner question John Ladasky <john_ladasky@sbcglobal.net> - 2013-06-04 00:53 -0700
RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 11:17 +0300
Re: Beginner question Anssi Saari <as@sci.fi> - 2013-06-04 10:45 +0300
Re: Beginner question John Ladasky <john_ladasky@sbcglobal.net> - 2013-06-04 00:57 -0700
Re: Beginner question Chris Angelico <rosuav@gmail.com> - 2013-06-04 18:24 +1000
Re: Beginner question Peter Otten <__peter__@web.de> - 2013-06-04 11:23 +0200
RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 14:23 +0300
Re: Beginner question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-04 12:35 +0000
RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 15:51 +0300
Re: Beginner question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-07 02:21 +0000
RE: Beginner question Fábio Santos <fabiosantosart@gmail.com> - 2013-06-04 12:34 +0100
RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 14:53 +0300
Re: Beginner question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-04 12:25 +0000
RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 15:37 +0300
RE: Beginner question Fábio Santos <fabiosantosart@gmail.com> - 2013-06-04 13:47 +0100
RE: Beginner question Fábio Santos <fabiosantosart@gmail.com> - 2013-06-04 13:15 +0100
Re: Beginner question Mitya Sirenef <msirenef@lightbird.net> - 2013-06-04 13:16 -0400
Re: Beginner question Larry Hudson <orgnut@yahoo.com> - 2013-06-04 02:13 -0700
Re: Beginner question Roy Smith <roy@panix.com> - 2013-06-04 09:16 -0400
Re: Beginner question Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-04 20:43 +0100
Re: Beginner question eschneider92@comcast.net - 2013-06-05 20:42 -0700
csiph-web