py编程石头剪刀布游戏
时间: 2024-01-06 08:05:42 浏览: 87
好的,以下是 Python 编写的石头剪刀布游戏代码:
```python
import random
player_score = 0
computer_score = 0
while True:
print("请出拳:石头【0】,剪刀【1】,布【2】")
player = int(input())
computer = random.randint(0, 2)
if player == 0:
if computer == 0:
print("平局")
elif computer == 1:
print("你赢了")
player_score += 1
else:
print("你输了")
computer_score += 1
elif player == 1:
if computer == 0:
print("你输了")
computer_score += 1
elif computer == 1:
print("平局")
else:
print("你赢了")
player_score += 1
else:
if computer == 0:
print("你赢了")
player_score += 1
elif computer == 1:
print("你输了")
computer_score += 1
else:
print("平局")
print("你的得分:", player_score)
print("计算机的得分:", computer_score)
if player_score == 3 or computer_score == 3:
break
if player_score > computer_score:
print("恭喜你获胜!")
else:
print("很遗憾,计算机获胜!")
```
这段代码使用了 `random` 模块来生成计算机随机出拳,使用 `while` 循环不断进行游戏,直到任意一方得分达到 3 分为止。最后根据得分比较输出胜负结果。
阅读全文