Untitled

mail@pastecode.io avatar
unknown
python
3 years ago
3.3 kB
4
Indexable
import sys #import sys to make sure sys.exit works

def RPSValidate(playerchoice):
    answers = ["rock","paper","scissors"] #make a list of the possible correct answers
    attempts_player = 0; #make a variable that holds the number of attempts for player 1
    while (playerchoice not in answers): #while player1 is not equal to the items in the list 'answers' 
        print('Player input is incorrect. Enter input again ') #print that input is incorrect
        playerchoice = input().lower() #take input again for player 1 and make it lowercase
        attempts_player += 1 #iterate the attempts_player1 variable by 1 for each time the loop runs 
        
        if (attempts_player == 3): #check to see if the attempts_player1 is equals to 3, if it is then uses sys.exit to exit the program.
            sys.exit("Player exceeded maximum number of tries.") #exits the program while displaying a message

    return playerchoice.lower()

def RPSExecute(player1, player2): 
    if (player1 == player2):
            print("The game is a draw because both player 1 and 2 chose:", player1) #print that the game is a draw and also the input of either player 1 or 2
            return 0
    
    elif (player1 == "rock"): #if player 1's input is 'rock' 
        if (player2 == "paper"): #and if player 2's input is 'paper' 
            print("Player 2 wins because they chose: ", player2) #print that player 2 wins because paper beats rock.
            return 2
        elif (player2 == "scissors"): #and if player 2's input is 'scissors'
            print("Player 1 wins because they chose: ", player1) #print that player 1 wins because rock beats scissors. 
            return 1
        
    elif (player1 == "paper"): #else if player 1's input is 'paper'
        if (player2 == "rock"): #and if player 2's input is 'rock'
            print("Player 1 wins because they chose: ", player1) #print that player 1 wins because paper beats rock. 
            return 1
        elif (player2 == "scissors"): #and if player 2's input is 'scissors'
            print("Player 2 wins because they chose: ", player2) #print that player 2 wins because scissors beats paper. 
            return 2
        
    elif (player1 == "scissors"): #if player 1's input is 'scissors'
        if (player2 == "rock"): #and if player 2's input is 'rock'
            print("Player 2 wins because they chose: ", player2) #print that player 2 wins because rock beats scissors. 
            return 2
        elif (player2 == "paper"): #and if player 2's input is 'paper'
            print("Player 1 wins because they chose: ", player1) #print that player 1 wins because scissors beats paper. 
            return 1 
        
        

print("Welcome to Rock, Paper, Scissors: ") #print an introductory message
print("Player 1, type 'rock', 'paper', or 'scissors'") #prompt Player 1 to type in their choice
player1 = input().lower() #declare player1 and take input from user and make it lowercase
player1 = RPSValidate(player1)



print("Player 2, type 'rock', 'paper', or 'scissors'") #prompt Player 2 to type in their choice
player2 = input().lower()
player2 = RPSValidate(player2)

RPSExecute(player1, player2)