Sure! Here's an example of a rock, paper, and scissor game in Python:
```python
import random
def get_user_choice():
choices = {"1": "rock", "2": "paper", "3": "scissor"}
while True:
choice = input("Enter your choice (1 - rock, 2 - paper, 3 - scissor): ")
if choice in choices:
return choices[choice]
else:
print("Invalid choice. Please try again.")
def get_computer_choice():
choices = ["rock", "paper", "scissor"]
return random.choice(choices)
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "It's a tie!"
elif (user_choice == "rock" and computer_choice == "scissor") or (user_choice == "paper" and computer_choice == "rock") or (user_choice == "scissor" and computer_choice == "paper"):
return "You won!"
else:
return "You lost!"
def play_again():
while True:
choice = input("Do you want to play again? (y/n): ")
if choice.lower() == "y":
return True
elif choice.lower() == "n":
return False
else:
print("Invalid choice. Please try again.")
def main():
print("Welcome to Rock, Paper, Scissor Game!")
while True:
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print(f"You chose {user_choice}, Computer chose {computer_choice}.")
print(determine_winner(user_choice, computer_choice))
if not play_again():
break
if __name__ == "__main__":
main()
```
With this code, the game will prompt the user to choose rock, paper, or scissor. The computer will also make a random choice. Then, it will determine the winner based on the user's and computer's choices. After each round, the user can choose to play again or quit the game.