单局石头剪刀布python程序
时间: 2024-10-16 17:10:04 浏览: 18
python实现石头剪刀布程序
单局石头、剪刀、布游戏的Python程序可以这样编写:
```python
import random
def game():
choices = ["石头", "剪刀", "布"]
player_choice = input("请输入你的选择(石头、剪刀或布):").lower()
if player_choice not in choices:
print("无效的选择,请重新输入")
return game()
computer_choice = random.choice(choices)
print(f"你选择了{player_choice},电脑选择了{computer_choice}")
if player_choice == computer_choice:
result = "平局"
elif (player_choice == "石头" and computer_choice == "剪刀") or \
(player_choice == "剪刀" and computer_choice == "布") or \
(player_choice == "布" and computer_choice == "石头"):
result = "你赢了"
else:
result = "你输了"
print(f"游戏结果是:{result}")
# 开始游戏
game()
```
这个程序首先定义了一个函数`game()`,用户会被提示输入他们的选择,然后电脑随机选择一个。接下来会比较两个选择,根据规则判断胜负,并打印出结果。
阅读全文