[Tutor] Paper Rock Scissors game - User's choice not returned properly
Peter Otten
__peter__ at web.de
Mon Oct 31 21:40:30 CET 2011
Joel Montes de Oca wrote:
> def UserChoice (): # The function that returns the choice from the
> user
> while 1:
>
> print 'Please select (P) for paper, (R) for Rock, or (S) for
> Scissors.'
> choice = raw_input('What is your selection?:')
> if choice.lower() in ('p', 'r','s'): # Converts the
> user's choice to lowercase and confirms the choice is valid
> return choice
> break
> else:
> print 'Try again.'
There's a problem with this code that I think nobody has addressed yet: if
you enter a lowercase letter, say "p", your function returns "p", if you
enter an uppercase "P" it returns "P". The calling code in
http://dpaste.com/644873/copy/
# Convert the letters to words
if UC == 'r':
UC = PRS[0]
elif UC == 'p':
UC = PRS[1]
elif UC == 's':
UC = PRS[2]
doesn't handle this case correctly. The best way is of course to normalize
the code returned by the UserChoice() function
...
choice = raw_input('What is your selection?:').lower()
if choice in ('p', 'r', 's'):
return choice
...
More information about the Tutor
mailing list