Skip to content

Instantly share code, notes, and snippets.

@nifo
Last active December 27, 2015 11:09
Show Gist options
  • Save nifo/7316187 to your computer and use it in GitHub Desktop.
Save nifo/7316187 to your computer and use it in GitHub Desktop.
A text version of the game rock, paper, scissors, lizard, spock. Idea from a stackoverflow question, he woundered how he could make his code more beautiful. I enjoyed making this little game one afternoon. stackoverflow Q: http://stackoverflow.com/questions/19584234/a-more-python-efficient-way-to-write-a-code/19592121
from random import randint
# ex. scissors and lizard is beaten by rock
beaten_by = {'rock': ['scissors', 'lizard'],
'paper': ['rock', 'spock'],
'scissors': ['paper', 'lizard'],
'lizard': ['spock', 'paper'],
'spock': ['scissors', 'rock']}
def rplsls_game():
"How to play:"
"You choose a weapon by typing its name or its corresponding number."
"The computer chooses a weapon at random."
"If your weapon, for example paper, is beaten by the computers weapon, scissor"
" the computer gains a point."
"Whoever leads by 2 points win."
player_score, computer_score = 0, 0
weapons = ['rock', 'paper', 'scissors', 'lizard', 'spock']
while(abs(player_score - computer_score) < 2):
print "-----------------------------------------------------------"
print " Please enter your selection: "
print " 1 (Rock), 2 (Paper), 3 (Scissors), 4 (Lizard) or 5 (Spock): """
computer_weapon = weapons[randint(0, 4)]
weapon = raw_input()
if weapon in '12345': weapon = weapons[int(weapon) - 1]
if weapon not in weapons:
print "invalid input"
continue
print "You selected: " + weapon
print "Computer selected: " + computer_weapon
if computer_weapon in beaten_by[weapon]:
print "You won!"
player_score += 1
elif weapon in beaten_by[computer_weapon]:
print "Computer won!"
computer_score += 1
else:
print "Draw!"
print "Player " + str(player_score) + " Computer: " + str(computer_score)
print "Congratulations! You won the game!" if player_score > computer_score else "Sorry, you lost :("
rplsls_game()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment