石头剪头布python
时间: 2024-07-03 19:01:19 浏览: 27
石头剪刀布是一个经典的游戏,可以用Python编写一个简单的命令行版本。这个游戏的基本规则是:玩家可以选择石头(Rock)、剪刀(Scissors)或布(Paper),根据游戏规则判断胜负。下面是创建一个基本的石头剪刀布游戏的简单示例:
```python
import random
def game():
options = ["rock", "paper", "scissors"]
player_choice = input("Please choose (rock, paper, scissors): ").lower()
# 确保玩家输入的是合法选项
while player_choice not in options:
player_choice = input("Invalid choice, please try again: ").lower()
computer_choice = random.choice(options)
print(f"Computer chose: {computer_choice}")
if player_choice == computer_choice:
result = "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"):
result = "You win!"
else:
result = "Computer wins!"
print(result)
# 开始游戏
game()
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)