用python编写一个人机猜拳游戏
时间: 2023-07-18 22:47:33 浏览: 73
好的,以下是一个简单的人机猜拳游戏的Python代码:
```python
import random
def game():
# 定义手势
gestures = ["石头", "剪刀", "布"]
# 人出拳
player = int(input("请出拳:0-石头,1-剪刀,2-布\n"))
print("你出了:", gestures[player])
# 机器出拳
computer = random.randint(0, 2)
print("机器出了:", gestures[computer])
# 判断胜负
if player == computer:
print("平局")
elif player == (computer + 1) % 3:
print("你赢了")
else:
print("你输了")
while True:
game()
again = input("再来一局?(y/n)")
if again.lower() != "y":
break
```
运行这段代码可以启动游戏,游戏规则为:玩家和机器分别出拳,判断胜负。
注意:以上代码仅供参考,可以根据自己的需求进行修改和完善。
相关问题
用python写一段人机猜拳游戏
好的,以下是一个简单的人机猜拳游戏的Python代码:
```python
import random
print("欢迎来到人机猜拳游戏!")
print("请出拳:1. 石头 2. 剪刀 3. 布")
while True:
player = int(input("请输入您的选择(1-3):"))
computer = random.randint(1, 3)
if player == 1:
print("您出了石头!")
elif player == 2:
print("您出了剪刀!")
elif player == 3:
print("您出了布!")
else:
print("输入错误,请重新出拳!")
continue
if computer == 1:
print("电脑出了石头!")
elif computer == 2:
print("电脑出了剪刀!")
elif computer == 3:
print("电脑出了布!")
if player == computer:
print("平局!")
elif player == 1 and computer == 2 or player == 2 and computer == 3 or player == 3 and computer == 1:
print("恭喜您获胜!")
else:
print("很遗憾,电脑获胜!")
choice = input("是否继续游戏?(y/n)")
if choice.lower() != 'y':
break
print("游戏结束!")
```
运行这段代码后,你可以和计算机进行多轮的猜拳游戏,直到你选择结束游戏为止。
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()
```
运行代码后,程序会让用户输入手势,然后随机生成计算机的手势,最后输出比赛结果。