Skip to content

Instantly share code, notes, and snippets.

@binki
Created May 1, 2016 02:17
Show Gist options
  • Save binki/237553697bd6993b6140b9e4820f2524 to your computer and use it in GitHub Desktop.
Save binki/237553697bd6993b6140b9e4820f2524 to your computer and use it in GitHub Desktop.
My “improved” version of ragespectate’s janken
#!/usr/bin/env python
import random
def intro():
print('Welcome to Rock, Paper, Scissors!')
def user():
user_move = ''
while user_move != 'r' and user_move != 'p' and user_move != 's':
user_move = input('What is your move? (r,p,s) ')
return user_move
def computer():
return random.choice(['s','r','p'])
def score(user_move, comp_move):
# confusing ridiculous for the fewer lines :-p
moves = 'rps'
user_minus_comp = (moves.index(user_move) - moves.index(comp_move)) % len(moves)
if user_minus_comp == 0:
return 0, 0
# e.g., user played paper and computer rock
if user_minus_comp == 1:
return 1, 0
return 0, 1
if __name__ == "__main__":
user_score = 0
comp_score = 0
intro()
while True:
user_move = user()
comp_move = computer()
score_delta = score(user_move, comp_move)
user_score += score_delta[0]
comp_score += score_delta[1]
print('You played %s. The computer played %s.' % (user_move, comp_move))
print(user_score, comp_score)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment