Advertisement
Matuiss2

Rock Paper Scissors, counting score

Oct 29th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. import random
  2.  
  3.  
  4. def player_choice(user_choice):  # Register and shows the players choice
  5.     if user_choice == 1:
  6.         print("The player chose rock")
  7.     elif user_choice == 2:
  8.         print("The player chose paper")
  9.     else:
  10.         print("The player chose scissors")
  11.  
  12.  
  13. def computer_choice(cpu_choice):  # Register and shows the cpus choice
  14.     if cpu_choice == 1:
  15.         print("The computer chose rock")
  16.     elif cpu_choice == 2:
  17.         print("The computer chose paper")
  18.     else:
  19.         print("The computer chose scissors")
  20.  
  21.  
  22. def result(user_choice, cpu_choice, player_score, cpu_score):  # Define the game result
  23.     if user_choice == cpu_choice:
  24.         return [player_score + 0.5, cpu_score + 0.5]
  25.     elif user_choice == 1 and cpu_choice == 3:
  26.         return [player_score + 1, cpu_score]
  27.     elif user_choice == 2 and cpu_choice == 1:
  28.         return [player_score + 1, cpu_score]
  29.     elif user_choice == 3 and cpu_choice == 2:
  30.         return [player_score + 1, cpu_score]
  31.     else:
  32.         return [player_score, cpu_score + 1]
  33.  
  34.  
  35. def print_score(p_score, c_score):  # Identifies the result and print the total score
  36.     print("Score:""\nPlayer:", p_score, "\nComputer:", c_score)
  37.  
  38.  
  39. def validation_input():  # Validates the input
  40.     while True:
  41.         try:
  42.             user_input = int(input("Put your choice:"))
  43.             if user_input not in range(1, 4):
  44.                 print("We only accept commands between 1 and 3, according to the table, type again")
  45.                 continue
  46.             if type(user_input) == int:
  47.                 break
  48.         except ValueError:
  49.             print("We only accept exact numbers")
  50.             continue
  51.     return user_input
  52.  
  53.  
  54. print('''1 - Rock
  55. 2 - Paper
  56. 3 - Scissors''')  # Printing the instructions
  57. human_score = 0
  58. computer_score = 0
  59. while True:  # The condition is not important since the loop will stop on line 68 if the user wishes so
  60.     user = validation_input()
  61.     player_choice(user)
  62.     ai = random.randint(1, 3)
  63.     computer_choice(ai)
  64.     human_score, computer_score = result(user, ai, human_score, computer_score)  # Accumulate the score
  65.     print_score(human_score, computer_score)
  66.     command = int(input("Type 0 to stop the program, type any another number to keep playing"))
  67.     if command == 0:
  68.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement