Advertisement
Guest User

procedural

a guest
Sep 8th, 2014
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1. import random, sys
  2.  
  3. def computer_choice():
  4.     computer_choice = random.choice("rps")
  5.     return computer_choice
  6.  
  7. def user_choice():
  8.     user_ch = raw_input(" Your Choice ?   ")
  9.     if user_ch != "r" and user_ch != "p" and user_ch != "s":
  10.         print " Wrong input, Please try again.\n"
  11.         user_ch = user_choice()
  12.        
  13.     return user_ch
  14.  
  15. def compare_choices(user_choice, computer_choice):
  16.     if user_choice == "r" and computer_choice == "p":
  17.         return "computer"
  18.     elif user_choice == "r" and computer_choice == "s":
  19.         return "user"
  20.     elif user_choice == "r" and computer_choice == "r":
  21.         return "n" # n means nul
  22.     elif user_choice == "p" and computer_choice == "r":
  23.         return "user"
  24.     elif user_choice == "p" and computer_choice == "s":
  25.         return "computer"
  26.     elif user_choice == "p" and computer_choice == "p":
  27.         return "n"
  28.     elif user_choice == "s" and computer_choice == "r":
  29.         return "computer"
  30.     elif user_choice == "s" and computer_choice == "p":
  31.         return "user"
  32.     elif user_choice == "s" and computer_choice == "s":
  33.         return "n"
  34.  
  35. def play_again():
  36.     print "Game Over"
  37.     choice = raw_input(" Would you like to play again? (Y/N) ")
  38.     if choice == "y" or choice == "Y":
  39.         play()
  40.     elif choice == "n" or choice == "N":
  41.         sys.exit()
  42.     else:
  43.         print " Wrong input, Please try again.\n"
  44.         play_again()
  45.  
  46.  
  47. def play():
  48.     global user_score, computer_score
  49.     i = 1
  50.     while i <= 3:
  51.         user_c = user_choice()
  52.         computer_c = computer_choice()
  53.         result = compare_choices(user_c, computer_c)
  54.         if result == "user":
  55.                 user_score += 1
  56.                 print " Your choice was " + user_c + ", the computer's choice was "+ computer_c
  57.                 print " You WON ! \n"
  58.         elif result == "computer":
  59.                 computer_score += 1
  60.                 print " Your choice was " + user_c + ", the computer's choice was "+ computer_c
  61.                 print " The computer won. \n"
  62.         else:
  63.                 print " Your choice was " + str(user_c) + ", the computer's choice was "+ str(computer_c)
  64.                 play_again()
  65.         i += 1
  66.  
  67.     print "Your score was: "+str(user_score)+", The Computer's score was: "+str(computer_score)
  68.     if user_score > computer_score:
  69.         print "You are the WINNER, CONGRATULATIONS! \n\n"
  70.     else:
  71.         print "The Computer is the WINNER. \n\n"
  72.        
  73.     play_again()
  74.  
  75. user_score = computer_score = 0
  76. print """
  77.          +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  78.          +                          WECLOME TO ROCK, PAPER, SISSORS GAME.                         +
  79.          +                                                                                                                                          +
  80.          + To test you luck, please choose "r" for Rock, "s" Sissors and "p" for paper.+
  81.          +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  82.         """
  83. play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement