python石头剪刀布记分
时间: 2023-07-01 17:29:16 浏览: 124
好的,下面是一个简单的 Python 石头剪刀布游戏并记分的代码:
```python
import random
# 定义游戏规则
rules = {'rock': 'scissors', 'paper': 'rock', 'scissors': 'paper'}
# 初始化游戏得分
score = {'player': 0, 'computer': 0}
# 游戏主循环
while True:
# 输出游戏得分
print("Player: {} Computer: {}".format(score['player'], score['computer']))
# 玩家出拳
player_choice = input("Please choose rock, paper or scissors: ").lower()
while player_choice not in rules:
player_choice = input("Invalid input! Please choose rock, paper or scissors: ").lower()
# 计算机出拳
computer_choice = random.choice(['rock', 'paper', 'scissors'])
# 输出玩家和计算机的选择
print("Player: {} Computer: {}".format(player_choice, computer_choice))
# 判断胜负并更新得分
if player_choice == computer_choice:
print("Tie!")
elif rules[player_choice] == computer_choice:
print("Player wins!")
score['player'] += 1
else:
print("Computer wins!")
score['computer'] += 1
# 判断是否继续游戏
if input("Do you want to continue? (y/n)").lower() != 'y':
break
```
在这个游戏中,玩家和计算机分别选择石头、剪刀或者布,然后比较其胜负,胜者得一分。游戏会不断进行,直到玩家选择停止。
阅读全文