• Advertisement
Sign in to follow this  

Rock Paper Scissors Lizard Spock in Python

This topic is 1923 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]



[CODE]
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")

[/CODE]

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
[quote name='afmack' timestamp='1349858195' post='4988645']
if (comp_number - player_number) % 5 == 1 or 2:
[/quote]

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'.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement