|
| 1 | +""" |
| 2 | +Rock, Paper, Scissors Game (CLI Version) |
| 3 | +Author: Your Name |
| 4 | +""" |
| 5 | + |
| 6 | +import random |
| 7 | + |
| 8 | + |
| 9 | +def get_user_choice(): |
| 10 | + """Prompt the user to enter their choice.""" |
| 11 | + choice = input("Enter your choice (rock, paper, scissors): ").lower() |
| 12 | + if choice in ["rock", "paper", "scissors"]: |
| 13 | + return choice |
| 14 | + else: |
| 15 | + print("Invalid choice! Please enter rock, paper, or scissors.") |
| 16 | + return get_user_choice() |
| 17 | + |
| 18 | + |
| 19 | +def get_computer_choice(): |
| 20 | + """Randomly select computer's choice.""" |
| 21 | + options = ["rock", "paper", "scissors"] |
| 22 | + return random.choice(options) |
| 23 | + |
| 24 | + |
| 25 | +def decide_winner(player, computer): |
| 26 | + """Decide the winner based on the choices.""" |
| 27 | + if player == computer: |
| 28 | + return "It's a draw!" |
| 29 | + elif ( |
| 30 | + (player == "rock" and computer == "scissors") |
| 31 | + or (player == "paper" and computer == "rock") |
| 32 | + or (player == "scissors" and computer == "paper") |
| 33 | + ): |
| 34 | + return "You win!" |
| 35 | + else: |
| 36 | + return "Computer wins!" |
| 37 | + |
| 38 | + |
| 39 | +def main(): |
| 40 | + """Main function to play the game.""" |
| 41 | + user_choice = get_user_choice() |
| 42 | + computer_choice = get_computer_choice() |
| 43 | + print(f"Computer chose: {computer_choice}") |
| 44 | + print(decide_winner(user_choice, computer_choice)) |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + main() |
0 commit comments