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

石头剪刀布游戏输出异常及回合计数问题求助

石头剪刀布游戏问题排查求助

我是编程新手,正在开发一款石头剪刀布(rock,paper,scissors)游戏,目前遇到两个问题:

  • 选择paper时无对战结果输出
  • 游戏回合数无法正确计数

以下是我的代码及运行示例,恳请帮助排查问题。

代码

import random

round = 1
win = 0
lose = 0
tie = 0


while True:
    choices = ["rock", "paper", "scissors"]

    computer = random.choice(choices)

    player = None

    while player not in choices:
        player = input("Rock, paper, or scissors??: ").lower()

    if player == computer:
        print("Player : ", player)
        print("Computer: ", computer)
        print("Tie!")
        tie+=1

    elif player == "rock":
        if computer == "paper":
            print("Player : ", player)
            print("Computer: ", computer)
            print("You lose")
            lose+=1

    elif player == "paper":
        if computer == "rock":
            print("Player : ", player)
            print("Computer: ", computer)
            print("You win")
            win+=1

    elif player == "scissors":
        if computer == "rock":
            print("Player : ", player)
            print("Computer: ", computer)
            print("You lose")
            lose+=1

    elif player == "rock":
        if computer == "scissors":
            print("Player : ", player)
            print("Computer: ", computer)
            print("You win")
            win+=1

    elif player == "paper":
        if computer == "scissors":
            print("Player : ", player)
            print("Computer: ", computer)
            print("You lose")
            lose+=1

    elif player == "scissors":
        if computer == "paper":
            print("Player : ", player)
            print("Computer: ", computer)
            print("You win")
            win+=1
    play_again = input("Would you like to play again? (yes/no): ").lower()

    if play_again != "yes":
        print("ROUNDS PLAYED: ",round)
        print("TOTAL WINS:",win)
        print("TOTAL LOSES: ",lose)
        print("TOTAL TIME PLAYER TIED WITH COMPUTER: ",tie)
        break
    elif play_again == "yes":
        rounds = round + 1
        print("NEW ROUND, ROUND: ",rounds)

print("Bye, Thanks for playing this program")

运行示例

Rock, paper, or scissors??: paper
 Would you like to play again? (yes/no): yes
 NEW ROUND, ROUND:  2
 Rock, paper, or scissors??: paper
 Would you like to play again? (yes/no): yes
 NEW ROUND, ROUND:  2
 Rock, paper, or scissors??: scissors
 Player :  scissors
 Computer:  scissors
 Tie!
 Would you like to play again? (yes/no): yes
 NEW ROUND, ROUND:  2
 Rock, paper, or scissors??: rock
 Player :  rock
 Computer:  rock
 Tie!
 Would you like to play again? (yes/no): yes
 NEW ROUND, ROUND:  2
 Rock, paper, or scissors??: paper
 Would you like to play again? (yes/no): no
 ROUNDS PLAYED:  1
 TOTAL WINS: 0
 TOTAL LOSES:  0
 TOTAL TIME PLAYER TIED WITH COMPUTER:  2
 Bye, Thanks for playing this program

问题分析与修正

问题1:选择paper时无对战结果输出

你的代码把同一玩家选择的不同电脑情况拆成了独立的elif分支,比如玩家选paper时,判断电脑是rock的分支和判断电脑是scissors的分支被其他elif隔开,且都是独立的elif player == "paper"。由于elif是互斥逻辑,只要前面的分支未匹配,后续同条件的elif也不会被触发,导致当玩家选paper且电脑是scissors时,没有对应的代码块执行,也就没有结果输出。

正确做法是将同一玩家选择的所有电脑情况整合到同一个elif块内,用嵌套的if/else处理。

问题2:回合数无法正确计数

你初始化的回合变量是round = 1,但玩家选择继续游戏时,你创建了临时变量rounds = round + 1,并未更新原有的round变量。这导致round始终保持初始值1,每次显示的新回合数只是临时计算的2,最终统计的总回合数也错误。

修正时直接更新原变量round += 1即可,另外建议不要用round做变量名,它是Python内置的四舍五入函数,容易引发冲突,可改为round_count。

修正后的完整代码

import random

round_count = 1
win = 0
lose = 0
tie = 0


while True:
    choices = ["rock", "paper", "scissors"]

    computer = random.choice(choices)

    player = None

    while player not in choices:
        player = input("Rock, paper, or scissors??: ").lower()

    if player == computer:
        print("Player : ", player)
        print("Computer: ", computer)
        print("Tie!")
        tie += 1
    elif player == "rock":
        if computer == "paper":
            print("Player : ", player)
            print("Computer: ", computer)
            print("You lose")
            lose += 1
        else:
            print("Player : ", player)
            print("Computer: ", computer)
            print("You win")
            win += 1
    elif player == "paper":
        if computer == "rock":
            print("Player : ", player)
            print("Computer: ", computer)
            print("You win")
            win += 1
        else:
            print("Player : ", player)
            print("Computer: ", computer)
            print("You lose")
            lose += 1
    elif player == "scissors":
        if computer == "rock":
            print("Player : ", player)
            print("Computer: ", computer)
            print("You lose")
            lose += 1
        else:
            print("Player : ", player)
            print("Computer: ", computer)
            print("You win")
            win += 1

    play_again = input("Would you like to play again? (yes/no): ").lower()

    if play_again != "yes":
        print("ROUNDS PLAYED: ", round_count)
        print("TOTAL WINS:", win)
        print("TOTAL LOSES: ", lose)
        print("TOTAL TIME PLAYER TIED WITH COMPUTER: ", tie)
        break
    else:
        round_count += 1
        print("NEW ROUND, ROUND: ", round_count)

print("Bye, Thanks for playing this program")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 01:55:19