import random # Check that users have entered a valid # option based on a list def string_checker(question, valid_ans): while True: error = f"Enter a valid option from {valid_ans}" user_response = input(question).lower() for item in valid_ans: # check if the user response is a valid answer if item == user_response: return item elif user_response == item[0]: return item print(error) print() def instructions(): print(''' **** Instructions **** To begin, choose the number of rounds (or press for infinite mode). Then play against the computer, You enter R (rock), P (paper) or S (scissors), The rules are: Paper beats rock Rock beats scissors Scissors beats paper Press xxx to end the game at any time ''' ) # compare the comp selection v the user input # result - win, lose or tie def rps_compare(user, comp): if user == comp: round_result = "tie" elif user == "paper" and comp == "rock": round_result = "win" elif user == "rock" and comp == "scissors": round_result = "win" elif user == "scissors" and comp == "paper": round_result = "win" else: round_result = "lose" return round_result # checks the input is an integer def int_check(question): while True: error = "Enter an integer more than 1" to_check = input(question) if to_check == "": return "infinite" try: response = int(to_check) # check the number is more than 1 if response >= 1: return response else: print(error) except ValueError: print(error) # Main routine # game variables mode = "regular" rounds_played = 0 rounds_tied = 0 rounds_lost = 0 rounds_won = 0 yes_no = ["yes","no"] rps_list = ["rock", "paper", "scissors", "xxx"] game_history = [] print() print(" โ›ฐ๏ธ ๐Ÿงป โœ‚๏ธ Rock / Paper / Scissors Game โ›ฐ๏ธ ๐Ÿ“ฐ โœ‚๏ธ ") print() print("** Welcome to the Rock, Paper, Scissors Game **") print() # instructions want_instructions = string_checker("Do you want to see the instructions? ", yes_no) if want_instructions == "yes": instructions() num_rounds = int_check("Enter the number of rounds or press enter for unlimited ") # infinite mode - increase number of rounds if num_rounds == "infinite": mode = "infinite" num_rounds = 5 # game loop starts here while rounds_played < num_rounds: # infinite mode if mode == "infinite": rounds_heading = f"\n*** Round {rounds_played + 1} (Infinite Mode) ***" else: rounds_heading = f"\n*** Round {rounds_played + 1} of {num_rounds} ***" print(rounds_heading) user_choice = string_checker("What do you choose: ", rps_list) # infinite mode - increase number of rounds if mode == "infinite": num_rounds += 1 if user_choice == "xxx": break # Random computer choice comp_choice = random.choice(rps_list[:-1]) result = rps_compare(user_choice, comp_choice) # Add counters for the result and the game history if result == "tie": rounds_tied += 1 feedback = "*** It's a tie! ***" elif result == "win": rounds_won += 1 feedback = "\U0001F607 \U0001F929 You won! \U0001F929 \U0001F607" else: rounds_lost += 1 feedback = "*** You lose! ***" 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 # infinite mode - increase number of rounds if num_rounds == "infinite": num_rounds += 1 if rounds_played > 0: # Calculate stats rounds_won = rounds_played - rounds_tied - rounds_lost percentage_won = rounds_won / rounds_played * 100 percentage_lost = rounds_lost / rounds_played * 100 percentage_tied = 100 - percentage_won - percentage_lost # Display Game Stats print() print("**** GAME STATS ****") print(f"You won {rounds_won} out of {rounds_played} rounds") print(f"Won: {percentage_won:.2f} % \t" f"Lost: {percentage_lost:.2f} % \t" f"Tied: {percentage_tied:.2f} % \t") print() # Game history option see_history = string_checker("Do you want to see your game history? ",yes_no) if see_history == "yes": for item in game_history: print(item) else: print("๐Ÿ˜พ๐Ÿ˜พ๐Ÿ˜พ Scaredy Cat! ๐Ÿ˜พ๐Ÿ˜พ๐Ÿ˜พ") # End of program message print() print("The program has ended")