python编写实现人机猜拳游戏
时间: 2023-06-29 09:12:17 浏览: 111
python实现猜拳游戏
好的,以下是一个 Python 代码实现人机猜拳游戏:
```python
import random
def play_game():
gestures = ['rock', 'paper', 'scissors']
computer_gesture = random.choice(gestures)
user_gesture = input("Please enter your gesture (rock/paper/scissors): ")
print("Computer's gesture: " + computer_gesture)
if computer_gesture == user_gesture:
print("It's a tie!")
elif (computer_gesture == 'rock' and user_gesture == 'scissors') or \
(computer_gesture == 'paper' and user_gesture == 'rock') or \
(computer_gesture == 'scissors' and user_gesture == 'paper'):
print("You lose!")
else:
print("You win!")
play_game()
```
运行代码后,程序会让用户输入手势,然后随机生成计算机的手势,最后输出比赛结果。
阅读全文