• Create Account

# Rock Paper Scissors Lizard Spock in Python

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1afmack  Members   -  Reputation: 111

Like
0Likes
Like

Posted 10 October 2012 - 02:36 AM

So, I'm doing this assignment and I cannot figure out what I'm doing wrong. I'm supposed to find the difference between the player's number and computer's number and then use modulus division. I then use the resulting number to decide on whether the computer or the player wins the hand. For some reason though python seems to be skipping my else statement or something. Any help would be appreciated.

def number_to_name(number):

if number == 0:
name = "rock"
return name

elif number == 1:
name = "Spock"
return name

elif number == 2:
name = "paper"
return name

elif number == 3:
name = "lizard"
return name

elif number == 4:
name = "scissors"
return name

def name_to_number(name):

if name == "rock":
number = 0
return number

elif name == "Spock":
number = 1
return number

elif name == "paper":
number = 2
return number

elif name == "lizard":
number = 3
return number

elif name == "scissors":
number = 4
return number

def rpsls(guess):

import random

player_number = name_to_number(guess)
comp_number = random.randrange(0, 5)
winner = (comp_number - player_number) % 5

print ""
print "Player chooses " + number_to_name(player_number)
print "Computer chooses " + number_to_name(comp_number)

if (comp_number - player_number) % 5 == 1 or 2:
print "Computer wins!"
elif (comp_number - player_number) % 5 == 0:
print "Player and computer tie!"
else:
print "Player wins!"

rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")



### #2PyroDragn  Members   -  Reputation: 404

Like
0Likes
Like

Posted 10 October 2012 - 02:50 AM

You say it seems to be skipping your else statement, can you say what the result of the program is, and what you would expect? Ie. Is it not running, is it giving incorrect win values?

### #3bambambam  Members   -  Reputation: 110

Like
2Likes
Like

Posted 10 October 2012 - 12:40 PM

if (comp_number - player_number) % 5 == 1 or 2:

This can't be executed correctly.
Firt you don't need to use the complete formula but just winner so you would get 'if winner == 1 or 2'
But then you see the real mistake, the or function can't be applied like that, you should write 'if winner == 1 or winner ==2'.

### #4Funker  Members   -  Reputation: 109

Like
0Likes
Like

Posted 14 October 2012 - 03:10 PM

thanks bambambam

[source lang="python"]def rpsls(guess): import random player_number = name_to_number(guess) comp_number = random.randrange(0, 5) winner = (comp_number - player_number) % 5 print "" print "Player chooses " + number_to_name(player_number) print "Computer chooses " + number_to_name(comp_number) if winner == 1 or winner == 2: print "Computer wins!" elif winner == 0: print "Player and computer tie!" else: print "Player wins!"[/source]

Edited by Funker, 14 October 2012 - 03:10 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS