python猜拳游戏编程代码
时间: 2023-09-25 15:15:43 浏览: 240
猜拳游戏代码
以下是一个简单的 Python 猜拳游戏代码示例:
```python
import random
# 定义猜拳手势
gestures = ['rock', 'paper', 'scissors']
# 询问用户出拳
user_choice = input("Please enter your choice (rock/paper/scissors): ").lower()
# 随机生成电脑出拳
computer_choice = random.choice(gestures)
# 输出电脑出拳结果
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!")
```
这个代码会首先询问用户输入他/她的出拳(rock/paper/scissors),然后随机生成电脑的出拳,最后根据规则判断胜负并输出结果。
阅读全文