import random def rps_compare(user, comp): # if computer and user choice are the same if user == comp: result = "tie" elif user == 'paper' and comp == 'rock': result = 'win' elif user == 'scissors' and comp == 'paper': result = 'win' elif user == 'rock' and comp == 'scissors': result = 'win' # if it's not win or tie its lose else: result = 'lose' return result # Check that users have entered a valid # option based on a list def string_checker(question, valid_ans=("yes", "no")): error = f'please enter a valid option from the following list: {valid_ans}' while True: #get user response and turn it lowercase user_response = input(question).lower() for item in valid_ans: # check if the user response is a word in the list if item == user_response: return item #check if the user response is the same as the first letter of an item in the list elif user_response == item[0]: return item #print error if user not enter correct print(error) print() # Check that users have entered a valid # option based on a list #checks that user enter an integer that is more than 0 def int_check(question): while True: error = "please enter an integer that is more than 1 " to_check = input(question) if to_check =="": return "infinite" try: response = int(to_check) #checks that the number is okay if response < 1: print(error) else: return response except ValueError: print(error) def instructions(): print(''' paper beat rock, rock best scissors, scissors beat paper. gl ''') # main routine starts here print(' rock papaer scissor game ') print() # intialise game varibles mode = 'regular' rounds_played = 0 rps_list = ["rock", "paper", "scissors", 'xxx'] #instructions ask if they want then display want_instructions = string_checker("Do you want to see the instructions? ") # checks users enter y or n if want_instructions == 'yes': instructions() #ask user for number of rounds or infintie mode num_rounds = int_check('How many rounds woul you like? pushe neter for infitine mode: ') if num_rounds == 'infinite': mode = 'infinite' num_rounds = 5 #game loop starts here while rounds_played < num_rounds: # rounds heading if mode == "infinite": rounds_heading = f"Round {rounds_played + 1} (Infinite Mode)" else: rounds_heading = f' Round {rounds_played + 1} of {num_rounds}' print(rounds_heading) print() # randomly choose from the rps list apart from exit code comp_choice = random.choice(rps_list[:-1]) print('computer choice', comp_choice) user_choice = string_checker('choose: ', rps_list) print('you chose', user_choice) #ending the infintie mode if user_choice == 'xxx': break result = rps_compare(user_choice, comp_choice) print(f"{user_choice} vs {comp_choice}, {result}") rounds_played += 1 # if users are infinite mode increase number of rounds if mode == 'infinite': num_rounds += 1 # game loop ends here #game history stats