用Python写一个石头剪刀布的游戏
时间: 2023-04-01 21:04:41 浏览: 159
好的,我可以回答这个问题。以下是一个简单的 Python 代码,可以实现石头剪刀布游戏:
```
import random
print("欢迎来到石头剪刀布游戏!")
print("请出拳:石头(1),剪刀(2),布(3)")
player = int(input("请输入你的选择(1/2/3):"))
computer = random.randint(1, 3)
if ((player == 1 and computer == 2)
or (player == 2 and computer == 3)
or (player == 3 and computer == 1)):
print("你赢了!")
elif player == computer:
print("平局!")
else:
print("你输了!")
print("电脑出的是:", computer)
```
希望这个代码对你有帮助!
相关问题
用python写一个石头剪刀布小游戏
好的,我可以给你写一个石头剪刀布小游戏。首先,你需要定义三个变量,分别是石头、剪刀和布:rock = '石头',scissors = '剪刀',paper = '布'。然后,你需要定义一个函数来比较两个变量,也就是玩家和电脑的出拳:def compare(player, computer):
if player == computer:
print('平局')
elif player == rock:
if computer == scissors:
print('玩家赢')
else:
print('电脑赢')
elif player == scissors:
if computer == paper:
print('玩家赢')
else:
print('电脑赢')
elif player == paper:
if computer == rock:
print('玩家赢')
else:
print('电脑赢')最后,你可以定义一个主程序,用于调用上面定义的函数:import randomwhile True:
player = input('请出拳(石头/剪刀/布): ')
computer = random.choice(['石头', '剪刀', '布'])
print('电脑出了:', computer)
compare(player, computer)
answer = input('要继续吗?(y/n)')
if answer == 'y':
continue
else:
print('游戏结束!')
break
用Python写一个石头剪刀布的代码
很高兴回答你的问题。以下是Python编写的石头剪刀布游戏代码:
```python
import random
print("欢迎来到石头剪刀布游戏!")
print("请出拳:石头(1)、剪刀(2)、布(3)")
while True:
player = int(input("请出拳:"))
computer = random.randint(1, 3)
if (player == 1 and computer == 2) or (player == 2 and computer == 3) or (player == 3 and computer == 1):
print("恭喜你,你赢了!")
elif player == computer:
print("平局,请再来一局!")
else:
print("很遗憾,你输了!")
```
希望这个代码能够帮助你玩石头剪刀布游戏。
阅读全文