import math import random # The key idea of this program is to equate the strings # "rock", "paper", "scissors", "lizard", "Spock" to numbers # as follows: # # 0 - rock # 1 - Spock # 2 - paper # 3 - lizard # 4 - scissors def number_to_name(number): if (number == 0): return "rock" elif (number == 1): return "Spock" elif (number == 2): return "paper" elif (number == 3): return "lizard" elif (number == 4): return "scissors" else: print "Enter 0-4" def name_to_number(name): if (name == "rock"): return 0 elif (name == "Spock"): return 1 elif (name == "paper"): return 2 elif (name == "lizard"): return 3 elif (name == "scissors"): return 4 else: print "enter 'rock', 'Spock', 'paper', 'lizard' or 'scissors'" def rpsls(name): # convert name to player_number using name_to_number player_number = name_to_number(raw_input("Choose Rock, Paper, Scissors, Lizard or Spock\n")) # compute random guess for comp_number using random.randrange() comp_number = random.randrange(0,4) # compute difference of player_number and comp_number modulo five res = (comp_number-player_number)%5 print "Player chooses " + number_to_name(player_number) print "" print "Computer chooses " + number_to_name(comp_number) print "" # use if/elif/else to determine winner if res == 1 or res == 2: print "Computer wins!" print "" print "" elif res == 3 or res == 4: print "Player wins!" print "" print "" else: print "Player and computer tie!" print "" print "" rpsls("Spock") rpsls("Rock") rpsls("Paper") rpsls("Lizard") rpsls("Scissors")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question