### Thread: Help on Paper/Rock/Scissors Game

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
6
Rep Power
0

#### Help on Paper/Rock/Scissors Game

Hey guys,

This is my first post on the forum. Could really use your help. I have a python program due tonight for a paper/rock/scissors game. For some reason, if the user selects rock the program works perfectly, but if the user chooses paper/scissors it keeps saying it is a tie. I'm sure there is a shorter, easier way to do this program, but my instructor wants us to do it using the stuff we learned in class last week. Here is my code:

import random

def userInput():
global selection
selection = str(raw_input("Rock, paper, or scissors?"))
if selection == ("rock") or ("Rock"):
selection = 1
elif selection == ("paper") or ("Paper"):
selection = 2
elif selection == ("Scissors") or ("scissors"):
selection = 3
return selection

def computation():
return random.randint(1,3)

def main():

computerScore = 0
userScore = 0
control_string = str("y")
while control_string == str("y"):
userSelection = userInput()
roll = computation()
if userSelection == roll:
print "Tie game"
elif userSelection == 1 and roll == 2:
print "Paper beats rock. You lose."
computerScore = computerScore + 1
elif userSelection == 1 and roll == 3:
print "Rock beats scisscors. You Win!"
userScore = userScore + 1
elif userSelection == 2 and roll == 3:
print "Sciscors beats paper. You lose."
computerScore = computerScore + 1
elif userSelection == 2 and roll == 1:
print "Paper beats rock. You win!."
userScore = userScore + 1
elif userSelection == 3 and roll == 1:
print "Rock beats Sciscors. You lose."
computerScore = computerScore + 1
elif userSelection == 3 and roll == 2:
print "Sciscors beats paper. You win!"
userScore = userScore + 1
print "Your score: ", userScore, "Computer's score: ", computerScore
control_string = str(raw_input("Would you like to run the program again? Please enter y for yes."))

main()

Thanks again.
Last edited by williamsjm5; April 14th, 2013 at 06:27 PM. Reason: indentation
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
6
Rep Power
0
Here is an example of the output:
Rock, paper, or scissors?paper
Paper beats rock. You lose.
Your score: 0 Computer's score: 1
Would you like to run the program again? Please enter y for yes.y
Rock, paper, or scissors?paper
Paper beats rock. You lose.
Your score: 0 Computer's score: 2
Would you like to run the program again? Please enter y for yes.
3. Code:
```import random

def userInput():

global selection  #### you return selection and capture the returned value.
################ selection should not be global

selection = str(raw_input("Rock, paper, or scissors?"))  ###### raw_input returns a string
selection = raw_input("Rock, paper, or scissors?")

if selection == ("rock") or ("Rock"): ################ 'Rock' is always true.  An empty string is the only False string
################ That 'Rock' is always true causes the symptom you described.
if (selection == "rock") or (selection == "Rock"): # this might be accepted---but only if everything else was perfect
if selection.lower() == 'rock':  # This version would be acceptable at a job interview

# experiment:  In the interpreter try
# >>> not 'Rock'
# ?
# >>> not not 'Rock'
# ?
# >>> not ''
# ?

selection = 1
elif selection == ("paper") or ("Paper"):
selection = 2
elif selection == ("Scissors") or ("scissors"):
selection = 3
return selection

def computation():
return random.randint(1,3)

def main():

computerScore = 0
userScore = 0

################ you inconsistently wrapped  strings with str.  Don't wrap known strings with str.
control_string = str("y")
while control_string == str("y"):

userSelection = userInput()
roll = computation()
if userSelection == roll:
print "Tie game"
elif userSelection == 1 and roll == 2:
print "Paper beats rock. You lose."
computerScore = computerScore + 1
elif userSelection == 1 and roll == 3:
print "Rock beats scisscors. You Win!"
userScore = userScore + 1
elif userSelection == 2 and roll == 3:
print "Sciscors beats paper. You lose."
computerScore = computerScore + 1
elif userSelection == 2 and roll == 1:
print "Paper beats rock. You win!."
userScore = userScore + 1
elif userSelection == 3 and roll == 1:
print "Rock beats Sciscors. You lose."
computerScore = computerScore + 1
elif userSelection == 3 and roll == 2:
print "Sciscors beats paper. You win!"
userScore = userScore + 1
print "Your score: ", userScore, "Computer's score: ", computerScore
control_string = str(raw_input("Would you like to run the program again? Please enter y for yes."))

main()```
Last edited by b49P23TIvg; April 14th, 2013 at 08:38 PM.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
6
Rep Power
0
Thanks for the help b4, here is the final code I just submitted for a fairly large portion of my grade for the class...

Code:
```#import random function

#define userInput function

def userInput():
global selection
#user input
selection = str(raw_input("Rock, paper, or scissors? "))
#convert any input into lowercase
if (selection.lower()) == ("rock"):
selection = 1
elif (selection.lower()) == ("paper"):
selection = 2
elif (selection.lower()) == ("scissors"):
selection = 3
#return selection value
return selection

#define computation function

def computation():
#generates a random number for the computer
import random
return random.randint(1,3)
#define main function
def main():
#declare score variables
computerScore = 0
userScore = 0
#declare control/loop variable
control_string = str("y")

while control_string == str("y"):
userSelection = userInput()
roll = computation()
if userSelection == roll:
print "Tie game"
if userSelection == 1 and roll == 2:
print "Paper beats rock. You lose."
computerScore = computerScore + 1
if userSelection == 1 and roll == 3:
print "Rock beats scisscors. You Win!"
userScore = userScore + 1
if userSelection == 2 and roll == 3:
print "Scissors beats paper. You lose."
computerScore = computerScore + 1
if userSelection == 2 and roll == 1:
print "Paper beats rock. You win!."
userScore = userScore + 1
if userSelection == 3 and roll == 1:
print "Rock beats Scissors. You lose."
computerScore = computerScore + 1
if userSelection == 3 and roll == 2:
print "Scissors beats paper. You win!"
userScore = userScore + 1
#print the score tally for the user and the program

#user input for control/string variable
control_string = str(raw_input("Go again? Enter y for yes."))
if control_string != "y":
print "Your score: ", userScore, "Computer's score: ", computerScore
main()```
5. Did your game work? If it worked that's excellent.

I didn't see this big problem until just now. Sorry. If the input is invalid the program fails because it hasn't associated a value with `selection'.
Code:
```>>> def f():
...  global v
...  return v
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in f
NameError: global name 'v' is not defined
>>>```
critique
Code:
```import random                             # do it, don't say it

# Your global selection variable is now useful.
selection = 0  #### selection must have a value.  The default is 0 meaning "invalid response"

#define userInput function                # comment is useless

def userInput():
'''
What is a good comment?  I liked explaining the variables.
I like explaining the algorithms.
This routine is so short that a simple explanation works.
You might not have yet studied __doc__ strings
"prompt user, convert the answer to an integer, and return it"
'''

global selection

answer = raw_input("Rock, paper, or scissors? ")
if answer_lower_case == "rock":       # don't parenthesize primary expression.
# Primary expressions are constants and names.
# exception, method of floating point is
# is reasonably less confusing with parentheses
# 3.0.as_integer_ratio()
# 3.0 . as_integer_ratio()
# (3.0).as_integer_ratio()
selection = 1
selection = 2
selection = 3
#return selection value  ################ useless comment
return selection

def computation():
#generates a random number for the computer
return random.randint(1,3)

def main():
#declare score variables
computerScore = 0                     # these are definitions, not declarations.
userScore = 0
#declare control/loop variable
control_string = "y"

while control_string == "y":
userSelection = userInput()
roll = computation()
if userSelection == roll:
print "Tie game"
if userSelection == 1 and roll == 2:
print "Paper beats rock. You lose."
computerScore = computerScore + 1
if userSelection == 1 and roll == 3:
print "Rock beats scisscors. You Win!"
userScore = userScore + 1
if userSelection == 2 and roll == 3:
print "Scissors beats paper. You lose."
computerScore = computerScore + 1
if userSelection == 2 and roll == 1:
print "Paper beats rock. You win!."
userScore = userScore + 1
if userSelection == 3 and roll == 1:
print "Rock beats Scissors. You lose."
computerScore = computerScore + 1
if userSelection == 3 and roll == 2:
print "Scissors beats paper. You win!"
userScore = userScore + 1
control_string = str(raw_input("Go again? Enter y for yes."))
### if control_string != "y":  ################# remove this line, you test at the top of the loop

# after loops finish, show the score.
print "Your score: ", userScore, "Computer's score: ", computerScore

main()```