1 Star 0 Fork 1

spyhooky/PythonGame

forked from 涛哥专属/PythonGame 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
rps_game.py 1.31 KB
一键复制 编辑 原始数据 按行查看 历史
lvjuntao 提交于 2024-11-05 13:44 . 初始化
import random
def get_user_choice():
while True:
user_choice = input("请选择(石头/剪刀/布):").lower()
if user_choice in ["石头", "剪刀", "布"]:
return user_choice
else:
print("无效的选择,请重新输入!")
def get_computer_choice():
choices = ["石头", "剪刀", "布"]
return random.choice(choices)
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "平局"
elif (
(user_choice == "石头" and computer_choice == "剪刀") or
(user_choice == "剪刀" and computer_choice == "布") or
(user_choice == "布" and computer_choice == "石头")
):
return "用户胜利"
else:
return "电脑胜利"
def play_game():
print("欢迎来到石头剪刀布游戏!")
while True:
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print(f"用户选择:{user_choice}")
print(f"电脑选择:{computer_choice}")
winner = determine_winner(user_choice, computer_choice)
print(f"结果:{winner}")
play_again = input("再玩一局?(是/否):").lower()
if play_again != "是":
break
print("谢谢游玩!")
play_game()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/spyhooky/python-game.git
[email protected]:spyhooky/python-game.git
spyhooky
python-game
PythonGame
master

搜索帮助