人机猜拳游戏python代码
时间: 2023-07-09 09:10:10 浏览: 111
好的,以下是一个简单的 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()
```
运行代码后,程序会让用户输入手势,然后随机生成计算机的手势,最后输出比赛结果。
阅读全文