You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python石头剪刀布游戏无法输出胜负结果问题求助

问题解决方案

你代码的核心问题是胜负判定环节错误引用了变量:你把存储玩家出拳结果的choice1、choice2,错写成了存储玩家姓名的name1、name2。只要两个玩家姓名不重复,所有判定条件都会直接匹配失败,自然不会输出胜负结果,直接跳到结束语句。

具体错误点

  • 平局判定逻辑错误:if name1 == name2是判断两个玩家名字相同,正确逻辑应为判断两人出拳一致
  • 平局输出内容错误:硬编码了{'r', 's', 'p'}集合,无法展示玩家实际选择的出拳
  • 全量判定条件变量引用错误:所有判断出拳的逻辑都误用了姓名变量,拿玩家姓名和'r'/'s'/'p'做匹配,自然不可能触发
  • 结果指代模糊:原有输出的"You win""You lose"没有明确指向哪个玩家,体验不佳

修正后完整代码

print("Welcome to Rock, Paper, Scissors!")
print("Let's Begin ...")
name1 = input("Player 1: What's your name? ")
name2 = input("Player 2: What's your name? ")

print("Hello " + name1 + " and " + name2 + "!")
print(name2 + ": Close your eyes!")

choice1 = input(name1 + ": enter 'r' for rock, 'p' for paper, and 's' for scissors: ")
print("Great choice! Now - cover your answer and ask " + name2 + " to choose. " )
choice2 = input(name2 + ": enter 'r' for rock, 'p' for paper, 's' for scissors: ")

# 修正变量引用为choice1、choice2
if choice1 == choice2:
    print(f"Both players selected {choice1}. It's a tie!")
elif choice1 == "r":
    if choice2 == "s":
        print(f"Rock smashes scissors! {name1} win!")
    else:
        print(f"Paper covers rock! {name2} win!")
elif choice1 == "p":
    if choice2 == "r":
        print(f"Paper covers rock! {name1} win!")
    else:
        print(f"Scissors cuts paper! {name2} win!")
elif choice1 == "s":
    if choice2 == "p":
        print(f"Scissors cuts paper! {name1} win!")
    else:
        print(f"Rock smashes scissors! {name2} win!")

print("Thanks for Playing Rock, Paper, Scissors")

内容的提问来源于stack exchange,提问作者Vo1d

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 06:15:04