6编写一个程序,模拟石头、剪刀、布的游戏,并决定谁是赢家。
时间: 2024-11-12 16:41:23 浏览: 12
【案例2-5】剪刀石头布.docx
为了编写一个简单的石头、剪刀、布游戏,我们可以创建一个交互式的命令行程序。以下是使用Python的一个简单实现:
```python
import random
def game():
choices = ["石头", "剪刀", "布"]
player_choice = input("玩家,请出拳(石头、剪刀、布): ").lower()
computer_choice = random.choice(choices)
print(f"电脑出了 {computer_choice}.")
if player_choice == computer_choice:
result = "平局"
elif (player_choice == "石头" and computer_choice == "剪刀") or \
(player_choice == "剪刀" and computer_choice == "布") or \
(player_choice == "布" and computer_choice == "石头"):
result = "玩家赢了!"
else:
result = "电脑赢了!"
print(result)
if __name__ == "__main__":
game()
阅读全文