写一个程序,让用户与计算机进行石头剪刀布游戏
时间: 2024-12-15 11:30:05 浏览: 39
你可以通过Python编写一个简单的命令行版的石头剪刀布游戏程序,以下是简单的代码示例:
```python
import random
def game():
choices = ["石头", "剪刀", "布"]
user_choice = input("请输入你的选择(石头、剪刀或布):").lower()
if user_choice not in choices:
print("无效的选择,请重新输入。")
return game()
computer_choice = random.choice(choices)
print(f"你选择了{user_choice},电脑选择了{computer_choice}.")
rules = {
("石头", "剪刀"): "赢",
("剪刀", "布"): "赢",
("布", "石头"): "赢",
("石头", "石头"): "平局",
("剪刀", "剪刀"): "平局",
("布", "布"): "平局"
}
result = rules.get((user_choice, computer_choice))
if result:
print(f"{result},你{('赢了', '输了')[user_choice != computer_choice]}!")
else:
print("系统错误,请检查电脑选择。")
if __name__ == "__main__":
game()
```
运行这个程序,用户可以在命令行输入他们的选择,然后电脑随机出拳,显示结果。
阅读全文
相关推荐


















