Last active
June 25, 2025 09:04
-
-
Save shinysu/4118833c0633d9d4c2472e00c31f2210 to your computer and use it in GitHub Desktop.
rock, paper, scissor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a simple Rock, Paper, Scissors game in Python | |
# where the computer's choice is fixed to "rock" for demonstration purposes. | |
# | |
# print, if-elif-else, input, and string comparison | |
computer_choice = "rock" | |
print('--' * 17) | |
print("Welcome to Rock, Paper, Scissors!") | |
print('--' * 17) | |
user_choice = input("Enter your choice (rock, paper, scissors): ") | |
if user_choice == computer_choice: | |
print("It's a tie!") | |
elif user_choice == "paper": | |
print("You win!") | |
else: | |
print("You lose!") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use list for storing choices | |
# | |
# list, random element in list, lower() | |
import random | |
choices = ["rock", "paper", "scissors"] | |
print("Welcome to Rock, Paper, Scissors!") | |
user_choice = input("Enter your choice (rock, paper, scissors): ").lower() | |
computer_choice = random.choice(choices) | |
if user_choice == computer_choice: | |
print("It's a tie!") | |
elif (user_choice == "rock" and computer_choice == "scissors") or \ | |
(user_choice == "paper" and computer_choice == "rock") or \ | |
(user_choice == "scissors" and computer_choice == "paper"): | |
print("You win!") | |
else: | |
print("You lose!") | |
print(f"Computer chose: {computer_choice}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use function to play the game | |
# function | |
import random | |
choices = ["rock", "paper", "scissors"] | |
def play_game(user_choice, computer_choice): | |
if user_choice == computer_choice: | |
print("It's a tie!") | |
elif (user_choice == "rock" and computer_choice == "scissors") or \ | |
(user_choice == "paper" and computer_choice == "rock") or \ | |
(user_choice == "scissors" and computer_choice == "paper"): | |
print("You win!") | |
else: | |
print("You lose!") | |
print("\nWelcome to Rock, Paper, Scissors!") | |
print('--' * 17) | |
user_choice = input("Enter your choice (rock, paper, scissors): ").lower() | |
computer_choice = random.choice(choices) | |
play_game(user_choice, computer_choice) | |
print(f"Computer chose: {computer_choice}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Check if user input is valid | |
# | |
import random | |
choices = ["rock", "paper", "scissors"] | |
def play_game(user_choice, computer_choice): | |
print(f"\nYou chose: {user_choice}") | |
print(f"Computer chose: {computer_choice}") | |
if user_choice == computer_choice: | |
print("\nIt's a tie!") | |
elif (user_choice == "rock" and computer_choice == "scissors") or \ | |
(user_choice == "paper" and computer_choice == "rock") or \ | |
(user_choice == "scissors" and computer_choice == "paper"): | |
print("\nYou win!") | |
else: | |
print("\nYou lose!") | |
print("\nWelcome to Rock, Paper, Scissors!") | |
print('--' * 17) | |
user_choice = input("Enter your choice (rock, paper, scissors): ").lower() | |
if user_choice not in choices: | |
print("Invalid choice! Please choose rock, paper, or scissors.") | |
exit() | |
computer_choice = random.choice(choices) | |
play_game(user_choice, computer_choice) | |
print("\nThanks for playing!") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Check if user input is valid | |
# | |
import random | |
choices = ["rock", "paper", "scissors"] | |
def play_game(user_choice, computer_choice): | |
print(f"\nYou chose: {user_choice}") | |
print(f"Computer chose: {computer_choice}") | |
if user_choice == computer_choice: | |
print("\nIt's a tie!") | |
elif (user_choice == "rock" and computer_choice == "scissors") or \ | |
(user_choice == "paper" and computer_choice == "rock") or \ | |
(user_choice == "scissors" and computer_choice == "paper"): | |
print("\nYou win!") | |
else: | |
print("\nYou lose!") | |
print("\nWelcome to Rock, Paper, Scissors!") | |
print('--' * 17) | |
while True: | |
user_choice = input("Enter your choice (rock, paper, scissors): ").lower() | |
if user_choice not in choices: | |
print("Invalid choice! Please choose rock, paper, or scissors.") | |
exit() | |
computer_choice = random.choice(choices) | |
play_game(user_choice, computer_choice) | |
play_again = input("Do you want to play again? (yes/no): ").lower() | |
if play_again != 'yes': | |
break | |
print("\nThanks for playing!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment