Hello everyone,
I am having a little trouble with a block of code that isn't behaving
the way I would expect. Maybe you can give me a hand and point where it
is going wrong.
The function that is not working correctly belongs to a Paper Rock
Scissor game I am making.
This particular function is responsible to:
a) Get the user's choice (Paper, Rock, or Scissors)
b) Return the user's choice within the variable choice to the
function that called it.
The function works correctly as long as the user _does not try to enter
a string other than_ 'P', 'R', or 'S'.
Logic:
Take the user's string and put it in the variable choice.
If choice is not 'P', 'R', or 'S'then pass a message to the user
and call the function again.
If the choice is 'P', 'R', or 'S'then return choice to where it was
called from.
The problem is this.
When the user enters a string other than the valid ones, the if
statements catches it and calls the same function again so the user can
enter a valid string. But the variable choice does not get assigned the
new string entered by the user, instead it is empty.
I would expect if the function runs again, the variable choice would be
updated to the last value given by the user.
The function: ( http://dpaste.com/644857/)
def UserChoice (): # The function that returns the choice from the user
print 'Please select (P) for paper, (R) for Rock, or (S) for Scissors.'
choice = raw_input('What is your selection?:')
if choice.lower() not in ('p', 'r','s'): # Converts the user's choice to lowercase and confirms the choice is valid
print 'I am sorry, you entered\'' + choice.upper() + '\' which is an invalid response. Please try again.'
raw_input('Press Enter to try again.')
UserChoice () # If the choice is not valid, run the function over
else:
return choice
Output with no problems: ( http://dpaste.com/644866/)
<http://dpaste.com/644866/>
Welcome to the PRS game.
Please select (P) for paper, (R) for Rock, or (S) for Scissors.
What is your selection?: s
You threw down Scissors while the computer threw down Rock
LOOSE
Output with problems: ( http://dpaste.com/644868/)
Welcome to the PRS game.
Please select (P) for paper, (R) for Rock, or (S) for Scissors.
What is your selection?: L
I am sorry, you entered 'L' which is an invalid response. Please try again.
Press Enter to try again.
Please select (P) for paper, (R) for Rock, or (S) for Scissors.
What is your selection?: s
You threw down None while the computer threw down Paper
LOOSE
Here's the full source code in case the problem is outside of the
function: ( http://dpaste.com/644873/)
import random # Import the random library
def main (): # The main function that gets called from main()
print '\nWelcome to the PRS game.\n'
choice = UserChoice () # Gets the choice from the user
cchoice = ComputerChoice () # Gets the choice from the computer
PrintOutcome (choice, cchoice) # Passes both choices to the PrintChoice function for display
return 0
def UserChoice (): # The function that returns the choice from the user
print 'Please select (P) for paper, (R) for Rock, or (S) for Scissors.'
choice = raw_input('What is your selection?:')
if choice.lower() not in ('p', 'r','s'): # Converts the user's choice to lowercase and confirms the choice is valid
print 'I am sorry, you entered\'' + choice.upper() + '\' which is an invalid response. Please try again.'
raw_input('Press Enter to try again.')
UserChoice () # If the choice is not valid, run the function over
else:
return choice
def ComputerChoice (): # The function that returns the choice from the computer
PRS = ['Rock', 'Paper', 'Scissors'] # A list of choices
cchoice = random.choice(PRS) # Choose a random item from the list
return cchoice # Return the function
def PrintOutcome (UC, CC): # Function responsable to display the choices and results
PRS = ['Rock', 'Paper', 'Scissors']
# Convert the letters to words
if UC == 'r':
UC = PRS[0]
elif UC == 'p':
UC = PRS[1]
elif UC == 's':
UC = PRS[2]
print '\n\tYou threw down', UC, 'while the computer threw down', CC +'\n'
# find the outcome of the game
if UC == CC:
print 'We have a tie, both players gain a point!'
elif UC == 'Rock' and CC == 'Scissors':
print 'WIN'
elif UC == 'Paper' and CC == 'Rock':
print 'WIN'
elif UC == 'Scissors' and CC == 'Paper':
print 'WIN'
else:
print 'LOOSE'
return 0
main()
I am using Python 2.7
I am running on Ubuntu 11.10
Thanks for any help
--
-Joel M.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111031/e16e2f16/attachment-0001.html>