Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FOR Rock paper scissor game using python #1842 #1988

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Mini-Projects/Python/Rock_paper_scissor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import random

def play_rock_paper_scissors():
"""Plays a game of rock paper scissors.

Returns:
A string indicating the winner of the game, or "Tie" if the game is a tie.
"""

# Get the user's choice.
user_choice = input("Rock, paper, or scissors? ")

# Validate the user's choice.
if user_choice not in ["rock", "paper", "scissors"]:
raise ValueError("Invalid choice.")

# Generate the computer's choice.
computer_choice = random.choice(["rock", "paper", "scissors"])

# Determine the winner.
if user_choice == computer_choice:
return "Tie"
elif user_choice == "rock" and computer_choice == "scissors":
return "You win!"
elif user_choice == "paper" and computer_choice == "rock":
return "You win!"
elif user_choice == "scissors" and computer_choice == "paper":
return "You win!"
else:
return "Computer wins!"


# Play the game.
winner = play_rock_paper_scissors()

# Print the winner.
print(winner)