用Python写一个石头剪刀布的代码
时间: 2023-03-30 19:03:03 浏览: 78
很高兴回答你的问题。以下是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("很遗憾,你输了!")
```
希望这个代码能够帮助你玩石头剪刀布游戏。
相关问题
用python写一段石头剪刀布的代码
好的,这是一段简单的石头剪刀布代码:
```
import random
print("欢迎来玩石头剪刀布游戏!")
print("请选择:石头(1),剪刀(2),布(3)")
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("电脑赢了!")
print("你出了:", end="")
if player == 1:
print("石头")
elif player == 2:
print("剪刀")
else:
print("布")
print("电脑出了:", end="")
if computer == 1:
print("石头")
elif computer == 2:
print("剪刀")
else:
print("布")
```
希望对你有帮助!
你能用python写一段石头剪刀布代码吗?
当然可以!以下是一个简单的石头剪刀布游戏代码示例:
```python
import random
#定义游戏规则
rules = {
"rock": "scissors",
"scissors": "paper",
"paper": "rock"
}
#定义游戏结果
results = {
"win": "You win!",
"lose": "You lose!",
"tie": "It's a tie!"
}
#用户输入
user_input = input("Enter rock, paper, or scissors: ").lower()
#计算机随机选择
computer_choice = random.choice(list(rules.keys()))
#判断游戏结果
if user_input == computer_choice:
result = "tie"
elif rules[user_input] == computer_choice:
result = "win"
else:
result = "lose"
#输出游戏结果
print("You chose:", user_input)
print("Computer chose:", computer_choice)
print(results[result])
```
运行代码后,你可以输入 "rock"、"paper" 或者 "scissors",然后与计算机进行一场石头剪刀布比赛!