石头剪刀布小游戏python
时间: 2024-01-08 15:21:26 浏览: 137
以下是一个简单的石头剪刀布小游戏的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)
print(determine_winner(user_choice, computer_choice))
play_game()
```
阅读全文