Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.56 KB | None | 0 0
  1. # Name: Marisa Tania
  2. # Class: F19 CS F021A 02W
  3. # Program: Rock, Paper, Scissors Game
  4.  
  5. # This is allows the user to play
  6. # Rock, Paper, Scissors Game
  7. # against the computer.
  8.  
  9. # Import random module.
  10. import random
  11.  
  12. def main():
  13.  
  14.     # 0 is a placeholder and will not be used.
  15.     player_wins = 0
  16.     computer_wins = 0
  17.  
  18.     # This loop will run the game 5 times that count
  19.     # and keep track of how many wins
  20.     # the player and the computer have.
  21.     while player_wins != 5 or computer_wins != 5:
  22.  
  23.         # Computer chooses a random number.
  24.         comp_number = random.randint(1, 3)
  25.  
  26.         # Initialize value of comp_choice variable
  27.         # corresponding to the comp_number value.
  28.         if comp_number == 1:
  29.             comp_choice = 'Rock'
  30.         elif comp_number == 2:
  31.             comp_choice = 'Paper'
  32.         else:
  33.             comp_choice = 'Scissor'
  34.  
  35.         # User enter their numeric choice from a menu.
  36.         user_number = int(input('Enter 1 for rock, 2 paper, 3 scissors: '))
  37.  
  38.         # Check user's input using a validation loop.
  39.         while user_number > 3 or user_number < 1:
  40.             print('Please choose a value of 1, 2, or 3')
  41.             user_number = int(input('Enter 1 for rock, 2 paper, 3 scissors: '))
  42.  
  43.         # Get the value in string.
  44.         choice_string = choiceString(user_number)
  45.  
  46.         # Get the computer's and user's choices.
  47.         rock_paper_scissors = rockPaperScissors(comp_choice, choice_string)
  48.  
  49.         # Display user's choice
  50.         print('You chose ' + choice_string)
  51.  
  52.         # Display computer's choice
  53.         print('Computer chose ' + comp_choice)
  54.  
  55.         # Determine and display who wins this game
  56.         # Calculate score
  57.         if rockPaperScissors == 1:
  58.             print ('The computer wins the game')
  59.         elif rockPaperScissors == 2:
  60.             print ('You win the game')
  61.         elif rockPaperScissors == 0:
  62.             print ('You made the same choice as computer. Starting over')
  63.             print ()
  64.  
  65.         # Calculate computer's and user's score
  66.         if rockPaperScissors == 1:
  67.             computer_wins = computer_wins + 1
  68.         elif rockPaperScissors == 2:
  69.             player_wins = player_wins + 1
  70.  
  71.         print("Player wins " + str(player_wins))
  72.         print("Player wins " + str(computer_wins))
  73.      
  74.     # Display score
  75.     print('After 5 runs, the totals are:')
  76.     print('You\t', player_wins)
  77.     print('Computer', computer_wins)
  78.  
  79. # The choiceString function receives the choice number
  80. # and returns that value in string
  81. def choiceString (choice):
  82.     if choice == 1:
  83.         return 'Rock'
  84.     elif choice == 2:
  85.         return 'Paper'
  86.     else:
  87.         return 'Scissor'
  88.    
  89. # The rockPaperScissors function receives numbers
  90. # representing the computer's and player's choices.
  91. def rockPaperScissors (computer, player):
  92.     if (player == 'Rock' and computer == 'Scissor'):
  93.         return 2
  94.     elif (player == 'Scissor' and computer == 'Paper'):
  95.         return 2
  96.     elif (player == 'Paper' and computer == 'Rock'):
  97.         return 2
  98.     elif (computer == 'Rock' and player == 'Scissor'):
  99.         return 1
  100.     elif (computer == 'Scissor' and player == 'Paper'):
  101.         return 1
  102.     elif (computer == 'Paper' and player == 'Rock'):
  103.         return 1
  104.     elif (computer == 'Rock' and player == 'Rock'):
  105.         return 0
  106.     elif (computer == 'Paper' and player == 'Paper'):
  107.         return 0
  108.     elif (computer == 'Scissor' and player == 'Scissor'):
  109.         return 0      
  110.            
  111.        
  112. # Call the main function
  113. main ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement