使用Python写一个猜拳游戏
时间: 2024-03-06 14:20:53 浏览: 108
python猜拳游戏
以下是一个简单的猜拳游戏的Python代码:
```python
import random
print("Welcome to the game of Rock, Paper, Scissors!")
while True:
player_choice = input("Please choose Rock (r), Paper (p), or Scissors (s): ")
if player_choice not in ['r', 'p', 's']:
print("Invalid input! Please try again.")
continue
computer_choice = random.choice(['r', 'p', 's'])
print("Computer chooses", computer_choice)
if player_choice == computer_choice:
print("It's a tie!")
elif (player_choice == 'r' and computer_choice == 's') \
or (player_choice == 'p' and computer_choice == 'r') \
or (player_choice == 's' and computer_choice == 'p'):
print("Congratulations! You win!")
else:
print("Sorry, you lose.")
play_again = input("Do you want to play again? (y/n): ")
if play_again != 'y':
break
print("Thanks for playing!")
```
该程序使用了一个无限循环,以便玩家可以选择继续玩下去或退出。在每次循环中,玩家输入他们的选择,计算机随机选择一个选项,并比较两个选择以决定获胜者。程序会不断循环,直到玩家选择退出。
阅读全文