Beginner question
Roy Smith
roy at panix.com
Tue Jun 4 09:16:20 EDT 2013
In article <IKKdneVcX7wyMjDMnZ2dnUVZ_sSdnZ2d at giganews.com>,
Larry Hudson <orgnut at 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.
More information about the Python-list
mailing list