编写程序模仿猜拳比赛python
时间: 2025-01-06 17:05:30 浏览: 1
编写一个简单的Python程序模拟猜拳游戏(石头、剪刀、布),你可以按照以下步骤操作:
```python
import random
# 定义猜拳的选项
choices = ["石头", "剪刀", "布"]
def game():
player_choice = input("请输入你的选择(石头、剪刀、布):")
computer_choice = random.choice(choices)
print(f"电脑选择了:{computer_choice}")
if player_choice == computer_choice:
result = "平局"
elif (player_choice == "石头" and computer_choice == "剪刀") or \
(player_choice == "剪刀" and computer_choice == "布") or \
(player_choice == "布" and computer_choice == "石头"):
result = "你赢了!"
else:
result = "你输了!"
print(f"结果是:{result}")
if __name__ == "__main__":
play_again = "yes"
while play_again.lower() == "yes":
game()
play_again = input("想再玩一次吗?(yes/no): ")
```
在这个程序中,用户输入他们的选择,然后计算机随机出拳。根据规则判断胜负,并询问用户是否想要再次玩游戏。
阅读全文