Python Program: Simple text-based Rock-Paper-Scissors game

Rock Paper-Scissors game

import random

def play_game(player_choice):
choices = [‘rock’, ‘paper’, ‘scissors’]
computer_choice = random.choice(choices)

print(“Player chooses:”, player_choice)
print(“Computer chooses:”, computer_choice)

if player_choice == computer_choice:
print(“It’s a tie!”)
elif (player_choice == ‘rock’ and computer_choice == ‘scissors’) or \
(player_choice == ‘paper’ and computer_choice == ‘rock’) or \
(player_choice == ‘scissors’ and computer_choice == ‘paper’):
print(“Player wins!”)
else:
print(“Computer wins!”)

def main():
print(“Welcome to Rock-Paper-Scissors!”)
print(“——————————-“)
print(“Choose your move:”)
print(“1. Rock”)
print(“2. Paper”)
print(“3. Scissors”)

choice = int(input(“Enter your choice (1-3): “))

if choice == 1:
play_game(‘rock’)
elif choice == 2:
play_game(‘paper’)
elif choice == 3:
play_game(‘scissors’)
else:
print(“Invalid choice. Please try again.”)

if __name__ == ‘__main__’:
main()

In this program, the play_game function takes the player’s choice as an argument and generates a random choice for the computer. It then compares the choices to determine the winner and displays the result.

The main function serves as the entry point of the program. It provides the game’s menu, allowing the player to choose between rock, paper, or scissors. Based on the player’s input, it calls the play_game function with the corresponding choice.

When you run the program, it will welcome the player to the Rock-Paper-Scissors game and display the menu of choices. The player can input their choice by entering a number from 1 to 3. The program will then determine the winner based on the player’s choice and the computer’s random choice. The result will be displayed on the screen.

Feel free to expand the game by adding more options or implementing additional features, such as keeping track of scores or allowing the player to play against a friend.

 

Top online courses in Teaching & Academics

Related Posts

Leave a Reply