pyton石头剪刀布人机对垒代码五局三胜
时间: 2023-06-02 13:03:57 浏览: 129
第6章 python元组及列表操作-python入门 python基础.pptx
好的,以下是五局三胜的Python石头剪刀布代码:
```
import random
# 人机对垒
def game(player,computer):
if player == computer:
print("平局,再来一局!")
return 0
elif (player == '石头' and computer == '剪刀') or (player == '剪刀' and computer == '布') or (player == '布' and computer == '石头'):
print("你赢了这局!")
return 1
else:
print("电脑赢了这局!")
return -1
# 主函数
if __name__ == '__main__':
win = 0
total = 0
while win < 3 and total < 5:
print("\n第{}局开始:".format(total+1))
player_hand = input("请出拳:(石头/剪刀/布)")
while player_hand not in ['石头', '剪刀', '布']:
player_hand = input("请出拳:(石头/剪刀/布)")
computer_hand = random.choice(['石头', '剪刀', '布'])
print("电脑出:", computer_hand)
result = game(player_hand, computer_hand)
if result == 1:
win += 1
elif result == -1:
win = 0
total += 1
if win == 3:
print("你赢了五局三胜的比赛!")
else:
print("电脑赢了五局三胜的比赛!")
```
希望能帮到你,如果有其他问题需要解答,请继续提问。
阅读全文