用python完成:制作一个自己与电脑进行剪刀石头布的游戏
时间: 2024-05-12 11:18:30 浏览: 175
Python 实现一个剪刀石头布游戏
好的,我可以帮您完成这个游戏!以下是Python代码示例:
```python
import random
# 定义电脑出拳的函数
def computer_choice():
choices = ["剪刀", "石头", "布"]
return random.choice(choices)
# 定义判断胜负的函数
def judge(player_choice, computer_choice):
if player_choice == computer_choice:
return "平局"
elif player_choice == "剪刀" and computer_choice == "布":
return "玩家胜利"
elif player_choice == "石头" and computer_choice == "剪刀":
return "玩家胜利"
elif player_choice == "布" and computer_choice == "石头":
return "玩家胜利"
else:
return "电脑胜利"
# 游戏主体
while True:
print("欢迎来玩剪刀石头布!")
player_choice = input("请输入您的选择(剪刀/石头/布):")
if player_choice not in ["剪刀", "石头", "布"]:
print("输入不合法,请重新输入!")
continue
computer_choice = computer_choice()
print("电脑出了:", computer_choice)
result = judge(player_choice, computer_choice)
if result == "平局":
print("平局!")
else:
print(result, "!")
play_again = input("是否再来一局?(是/否):")
if play_again == "否":
print("感谢您的游玩,下次再见!")
break
```
希望这个代码能够满足您的需求!
阅读全文