用play_game写一段猜拳游戏的pyhton代码
时间: 2023-06-04 20:07:53 浏览: 176
好的,以下是用 Python 编写的一个简单的猜拳游戏代码:
```python
import random
def play_game():
options = ['rock', 'paper', 'scissors']
computer_choice = random.choice(options)
user_choice = input("Please choose rock, paper or scissors: ")
print("You chose:", user_choice)
print("Computer chose:", computer_choice)
if user_choice == computer_choice:
print("It's a tie!")
elif (user_choice == 'rock' and computer_choice == 'scissors') or (user_choice == 'paper' and computer_choice == 'rock') or (user_choice == 'scissors' and computer_choice == 'paper'):
print("You win!")
else:
print("Computer wins!")
play_game()
```
调用 play_game() 函数即可开始游戏。每次游戏都随机生成电脑的出拳,用户通过输入 'rock'、'paper' 或 'scissors' 进行猜拳,程序会根据规则判断胜负并输出结果。
阅读全文