编写python实现猜拳游戏
时间: 2024-12-19 13:20:31 浏览: 5
编写Python实现猜拳游戏,通常我们会选择石头、剪刀、布(Rock, Paper, Scissors)作为游戏元素。下面是一个简单的猜拳游戏示例:
```python
import random
def game():
options = ['rock', 'paper', 'scissors']
player_choice = input("请输入你的选择(rock/paper/scissors):")
while not player_choice.lower() in options:
print("无效的选择,请输入 rock, paper 或 scissors.")
player_choice = input("请输入你的选择:")
computer_choice = random.choice(options)
print(f"你选择了{player_choice},电脑选择了{computer_choice}.")
if player_choice == computer_choice:
result = "平局"
elif (player_choice == 'rock' and computer_choice == 'scissors') or \
(player_choice == 'paper' and computer_choice == 'rock') or \
(player_choice == 'scissors' and computer_choice == 'paper'):
result = "你赢了!"
else:
result = "你输了!"
print(result)
if __name__ == "__main__":
game()
```
在这个游戏中,玩家输入他们的选择,然后电脑随机选择。比较两个选择后,会给出胜负结果。运行此程序,你就可以体验猜拳的乐趣。
阅读全文