Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Rock, Paper, Scissors

By: AlbionsRefuge on Jul 11th, 2013  |  syntax: Python  |  size: 1.54 KB  |  views: 69  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # Exercise 1.7 – Rock, Paper, Scissors
  2.  
  3. # function to keep nagging to get a good choice
  4. def get_player_choice(player_name):
  5.     bad_choice = True
  6.     good_choices = ["rock", "paper", "scissors"]
  7.    
  8.     while bad_choice:
  9.         choice = raw_input(player_name + ": rock, paper or scissors? ")
  10.         if choice in good_choices:
  11.             bad_choice = False
  12.             return choice
  13.         else:
  14.             print "This is not a valid object selection\n"
  15.             bad_choice = True
  16.            
  17.  
  18. # function to compare the choices
  19. def check_outcome(choice1, choice2):
  20.     print choice1, choice2
  21.     if choice1 == choice2:
  22.         return "Tie!"
  23.     elif choice1 == 'rock':
  24.         if choice2 == 'scissors':
  25.             return "Player 1 wins"
  26.         else:
  27.             return "Player 2 wins"
  28.     elif choice1 == 'paper':
  29.         if choice2 == 'rock':
  30.             return "Player 1 wins"
  31.         else:
  32.             return "Player 2 wins"
  33.     elif choice1 == 'scissors':
  34.         if choice2 == 'rock':
  35.             return "Player 2 wins"
  36.         else:
  37.             return "Player 1 wins"
  38.     else:
  39.         return "I'm confused"
  40.  
  41. # main part of the program
  42. play_again = True
  43.  
  44. while play_again:
  45.     play_again = raw_input("\nReady to play? (y/n) ").lower()
  46.     if play_again in ["y", "yes"]:
  47.         play_again = True
  48.         player1 = get_player_choice("Player1")
  49.         player2 = get_player_choice("Player2")
  50.         print check_outcome(player1, player2)
  51.     else:
  52.         play_again = False
  53.         print "See you next time!"