猜拳游戏python代码
时间: 2024-05-02 13:15:19 浏览: 19
当然,我可以给你介绍一下猜拳游戏的Python代码。以下是一个简单的示例:
```python
import random
def get_user_choice():
choice = input("请选择(剪刀/石头/布):")
while choice not in ["剪刀", "石头", "布"]:
choice = input("请选择(剪刀/石头/布):")
return choice
def get_computer_choice():
choices = ["剪刀", "石头", "布"]
return random.choice(choices)
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "平局"
elif (user_choice == "剪刀" and computer_choice == "布") or \
(user_choice == "石头" and computer_choice == "剪刀") or \
(user_choice == "布" and computer_choice == "石头"):
return "你赢了"
else:
return "你输了"
def play_game():
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print("你选择了:", user_choice)
print("电脑选择了:", computer_choice)
result = determine_winner(user_choice, computer_choice)
print(result)
play_game()
```
这个代码实现了一个简单的猜拳游戏。用户通过输入选择剪刀、石头或布,电脑随机生成一个选择,然后根据规则判断胜负并输出结果。