循环猜拳游戏代码python
时间: 2024-05-13 12:10:25 浏览: 171
Python代码-猜拳游戏.py
当然可以!下面是一个简单的循环猜拳游戏的Python代码示例:
```python
import random
def play_game():
choices = ['石头', '剪刀', '布']
user_score = 0
computer_score = 0
while True:
user_choice = input("请输入你的选择(石头/剪刀/布):")
computer_choice = random.choice(choices)
print("你选择了:", user_choice)
print("电脑选择了:", computer_choice)
if user_choice not in choices:
print("请输入有效的选择!")
continue
if user_choice == computer_choice:
print("平局!")
elif (user_choice == '石头' and computer_choice == '剪刀') or \
(user_choice == '剪刀' and computer_choice == '布') or \
(user_choice == '布' and computer_choice == '石头'):
print("你赢了!")
user_score += 1
else:
print("你输了!")
computer_score += 1
print("当前得分:你", user_score, "电脑", computer_score)
play_again = input("是否继续游戏?(是/否)")
if play_again.lower() != '是':
break
play_game()
```
这个代码实现了一个简单的循环猜拳游戏。玩家和电脑轮流出拳,根据规则判断胜负,并记录双方的得分。玩家可以选择是否继续游戏。
阅读全文