import random # Check that users enter a valid # option based on a list def string_checker(question, valid_ans=("yes", "no")): error = f"Please enter a option from the following list: {valid_ans}" while True: # Get user response and make sure it is lowercase user_response = input(question).lower() for item in valid_ans: # check if the users response is a word in the list if item == user_response: return item # check if the users response is the same as # the first letter of an item in the list elif user_response == item[0]: return item # Print error f user does not enter a valid response print(error) print() # Checks that the user enters an integer more than 0 (allows ) def int_check(question, low, high): while True: error = "please input and integer (whole number) equal to 1 or more, and 20 or less. " to_check = input(question) try: response = int(to_check) #checks that their number is more than / equal to {low} and less than / equal to {high} if response >= low and response >=high: print(error) else: return response except ValueError: print(error) # Compare user/computer choice and returns # Result (win / lose / tie) def rps_compare(user, comp): # If the user and computer choice is the same it is a tie if user == comp: round_result = 'tie' # there are three ways to win elif user == "paper" and comp == "rock": round_result = "Player wins" elif user == "scissors" and comp == "paper": round_result = "Player wins" elif user == "rock" and comp == "scissors": round_result = "Player wins" # If it is not a win / tie, the computer wins else: round_result = "Computer wins" return round_result # Main routine starts here # Initialise game variables mode = "regular" rounds_played = 0 rounds_tried = 0 rounds_lost = 0 rps_list = ["rock", "paper", "scissors", "xxx"] game_history = [] print("Rock/Paper/Scissors game") print() def instructions(): print() print("Rock/Paper/Scissors game") # Ask the user if they want to read the instructions (check if they want to using yes/no) want_instructions = string_checker(f'Would you like to see the instructions?').lower() # Display the instructions if the user agreed to seeing them... if want_instructions == "yes": """Prints instructions""" print(""" ****Instructions**** To begin, choose the number of round you would like to play (or begin infinite mode) Then play against the computer. You need to choose from the following: P(Paper), S(Scissors), or R(Rock). the rules are as follows: o Paper beats rock o rock beats scissors o scissors beats paper Enter if you would like to exit Good luck! """) # Ask for the number of round they would like to play num_rounds = int_check("How many rounds would you like to play? Please choose a number of round between / equal to 1 and 20", 1, 20) #Game loop starts here while rounds_played < num_rounds: # rounds headings rounds_heading = f"\n Round {rounds_played + 1} of {num_rounds}" print(rounds_heading) # Randomly choose from rps list comp_choice = random.choice(rps_list[:-1]) # gets user choice user_choice = string_checker("choose:", rps_list) print("you chose", user_choice) # if user choice is the exit code, break the loop if user_choice == "xxx": break result = rps_compare(user_choice, comp_choice) # Ajust game lost / game tried and adds results to the game history if result == "tie": rounds_tried += 1 feedback = "It's a tie!" elif result == "Computer wins": rounds_lost += 1 feedback = "Computer wins" else: feedback = "You won" # set up round feedback and output it user # add it to the game hisotry list (include the round number) round_feedback = f"{user_choice} vs {comp_choice}, {feedback}" history_item = f"Round: {rounds_played + 1} - {round_feedback}" print(round_feedback) game_history.append(history_item) rounds_played += 1 # game loop ends here # game history / statistics area if rounds_played > 0: # calculate statistics rounds_won = rounds_played - rounds_tried - rounds_lost percent_won = rounds_won / rounds_played * 100 percent_lost = rounds_lost / rounds_played * 100 percent_tied = rounds_tried / rounds_played * 100 # output game statistics print("Game Statistics") print(f"Won: {percent_won:.2f} \t" f"Lost: {percent_lost:.2f} \t" f"Tied: {percent_tied:.2f}") # Ask user if they want to see thier game history and output if requested. see_history = string_checker("\nDo you want to see your game history?") if see_history == "yes": for item in game_history: print(item) print() print("Thank you for playing") else: print("Oops, you chickened out!")