用python做一个对局猜拳小游戏
时间: 2023-05-17 15:05:19 浏览: 211
python实现人机猜拳小游戏
5星 · 资源好评率100%
好的,以下是一个简单的对局猜拳小游戏的 Python 代码:
```python
import random
def play_game():
choices = ['rock', 'paper', 'scissors']
computer_choice = random.choice(choices)
user_choice = input("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':
print("You win!")
elif user_choice == 'paper' and computer_choice == 'rock':
print("You win!")
elif user_choice == 'scissors' and computer_choice == 'paper':
print("You win!")
else:
print("Computer wins!")
play_game()
```
你可以在命令行中运行这个代码,然后按照提示输入你的选择,程序会随机生成计算机的选择并判断胜负。
阅读全文