猜拳游戏Python输出结果
时间: 2024-06-14 20:01:57 浏览: 94
python猜拳游戏
猜拳游戏(Rock, Paper, Scissors)是一个常见的交互式游戏,玩家和电脑随机选择石头、剪刀或布进行对决。在Python中,你可以创建一个简单的命令行版本的游戏。以下是一个基础的实现例子:
```python
import random
def play_rps():
choices = ['rock', 'paper', 'scissors']
player_choice = input("Your choice (rock/paper/scissors): ").lower()
if player_choice not in choices:
print("Invalid choice! Please choose rock, paper, or scissors.")
return play_rps()
computer_choice = random.choice(choices)
print(f"Computer chose: {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("You win!")
else:
print("You lose!")
play_rps()
```
当你运行这个程序时,会提示你输入你的选择,然后电脑随机选择。最后,根据游戏规则判断胜负并输出结果。
阅读全文