用python写一段石头剪刀布的代码
时间: 2023-02-26 08:46:09 浏览: 112
好的,这是一段简单的石头剪刀布代码:
```
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",然后与计算机进行一场石头剪刀布比赛!
python 石头剪刀布
好的,我可以为您编写一个简单的石头剪刀布游戏。以下是示例代码:
```python
import random
def play_game():
choices = ["石头", "剪刀", "布"] # 定义选项
computer_choice = random.choice(choices) # 计算机随机选择
user_choice = input("请选择石头、剪刀或布:") # 用户输入选择
print("您选择了:", user_choice)
print("计算机选择了:", computer_choice)
if user_choice not in choices:
print("无效的选择,请重新选择。")
play_game()
elif user_choice == computer_choice:
print("平局!")
elif (
(user_choice == "石头" and computer_choice == "剪刀")
or (user_choice == "剪刀" and computer_choice == "布")
or (user_choice == "布" and computer_choice == "石头")
):
print("您赢了!")
else:
print("您输了!")
play_game()
```
运行这段代码,您可以选择石头、剪刀或布,然后与计算机进行比较,看看谁赢了。请注意,这只是一个简单的示例,您可以根据自己的需求进行修改和扩展。
阅读全文
相关推荐
















