Untitled

mail@pastecode.io avatar
unknown
python
3 years ago
3.0 kB
2
Indexable
Never
import sys #import sys to make sure sys.exit works
attempts_player1 = 0;
attempts_player2 = 0;

def RPSValidate(playerchoice):
    answers = ["rock","paper","scissors"] #make a list of the possible correct answers
    if playerchoice not in answers:
        return ("input error")
    else:
        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)

while player1 == "input error":
    player1 = input().lower()
    player1 = RPSValidate(player1)
    attempts_player1  += 1
    if attempts_player1 == 3:
        sys.exit()

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

while player2 == "input error":
    player2 = input().lower()
    player2 = RPSValidate(player2)
    attempts_player2  += 1
    if attempts_player2 == 3:
        sys.exit()
        
RPSExecute(player1, player2)