Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.0 kB
1
Indexable
Never
import sys  # Importing sys module for exit codes

# Getting player names
player_1 = input("Welcome to my Rock Paper Scissors simulator! Enter Player One's name: ")
player_2 = input("Enter Player Two's name: ")

# Function to get the hand choice
def hand_choice(player_name):
    while True:
        hand = input(f"\n{player_name}, what hand would you like to play (rock, paper, or scissors)? ").strip().lower()
        if hand in ["rock", "paper", "scissors"]:
            return hand
        else:
            print("Invalid choice. Please enter rock, paper, or scissors.")

# Function to determine the winner
def calculate_winner(hand1, hand2):
    if hand1 == hand2:
        return None  # It's a tie, no winner yet
    elif (hand1 == 'rock' and hand2 == 'scissors') or \
         (hand1 == 'scissors' and hand2 == 'paper') or \
         (hand1 == 'paper' and hand2 == 'rock'):
        return player_1
    else:
        return player_2

# Main function to run the game
def main():

    winner = None
    while winner is None:
        # Player 1's choice
        player_1_hand = hand_choice(player_1)
        
        # Clear screen to prevent cheating (optional)
        print("\n" * 50)
        print("No cheating.\n" * 3)
        
        # Player 2's choice
        player_2_hand = hand_choice(player_2)

        # Determine the winner
        winner = calculate_winner(player_1_hand, player_2_hand)
        
        print(f"\n{player_1} chose: {player_1_hand}")
        print(f"{player_2} chose: {player_2_hand}")
        print(f"\nResult: {winner} wins!")
    # Decide exit code based on winner
    if winner == player_1:
        sys.exit(f"Game over! {player_1} wins. Thank you for playing my game.")
    elif winner == player_2:
        sys.exit(f"Game over! {player_2} wins. Thank you for playing my game.")
    else:
        sys.exit("Game over! It's a tie. Thank you for playing my game.")

if __name__ == "__main__": 
    main()
Leave a Comment