Python猜拳游戏五局三胜
时间: 2023-11-29 08:45:02 浏览: 225
以下是Python实现猜拳游戏五局三胜的代码:
```python
import random
# 定义猜拳胜利条件
win_list = [['石头', '剪刀'], ['剪刀', '布'], ['布', '石头']]
# 定义判断胜负的函数
def judge(player, computer):
if player == computer:
return '平局'
elif [player, computer] in win_list:
return '你赢了'
else:
return '你输了'
# 定义主函数
def main():
# 定义计数器
player_win = 0
computer_win = 0
# 获取游戏局数
total_rounds = int(input('请输入游戏局数:'))
# 判断游戏局数是否合法
while total_rounds % 2 == 0 or total_rounds < 1:
total_rounds = int(input('游戏局数必须为正奇数,请重新输入:'))
# 开始游戏
while player_win < (total_rounds + 1) / 2 and computer_win < (total_rounds + 1) / 2:
# 获取玩家输入
player = input('请出拳(石头/剪刀/布):')
# 判断玩家输入是否合法
while player not in ['石头', '剪刀', '布']:
player = input('输入有误,请重新输入(石头/剪刀/布):')
# 获取电脑随机出拳
computer = random.choice(['石头', '剪刀', '布'])
# 输出结果
print('你出了%s,电脑出了%s,%s' % (player, computer, judge(player, computer)))
# 更新计数器
if judge(player, computer) == '你赢了':
player_win += 1
elif judge(player, computer) == '你输了':
computer_win += 1
# 输出当前比分
print('当前比分:%d:%d' % (player_win, computer_win))
# 判断最终胜负
if player_win > computer_win:
print('你赢了!')
else:
print('你输了!')
if __name__ == '__main__':
main()
```
阅读全文