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

Python石头剪刀布游戏如何实现胜负平结果累计统计

问题根因

统计无法累计的核心原因有两个:

  • 你定义的fullgame()内部嵌套的game()函数虽然会返回单局结果(w代表胜、l代表负、d代表平),但不管是内层的game()调用还是fullgame()本身,都没有把这个结果向外传递,单局结果跑完就被丢弃了
  • 你只定义了总场次gamesplayed的累计变量,没有初始化胜、负、平对应的计数变量,单局结果没有持久存储的位置,自然没法跨对局累计
修复步骤
  • 在主循环外层,和总场次变量一起初始化三个结果计数变量
  • 给fullgame()加返回值,把内部单局判断的结果传到函数外层
  • 主循环调用fullgame()时接收返回的单局结果,对应更新计数
  • 对局结束打印统计时,同步输出胜、负、平的累计值
修复后完整代码
# Rock, Paper, Scissors!

import random
import time

print('Welcome to Rock, Paper, Scissors, a Python recreation of the classic game.')
print('The computer will randomly choose a sign at the start of the run for you to try to beat.')
print('Choose carefully...')


def fullgame():
    compguess = random.randint(1, 3)
    if compguess == 1:
        compsign = 'rock'
    elif compguess == 2:
        compsign = 'paper'
    else:
        compsign = 'scissors'
    playsign = input('Would you like to throw rock, paper or scissors? ')
    while playsign != 'rock' and playsign != 'paper' and playsign != 'scissors':
        print('You need to pick a valid sign!')
        playsign = input('Would you like to throw rock, paper or scissors? ')

    def numberallocator(rps):
        if rps == 'rock':
            return 1
        elif rps == 'paper':
            return 2
        elif rps == 'scissors':
            return 3
        else:
            print('That\'s not a valid sign!')
    playguess = numberallocator(playsign)

    def game(cg, pg):
        print('The computer is deciding...')
        time.sleep(1)
        print('The computer throws ' + compsign + ' and you throw ' + playsign + '...')
        time.sleep(1)
        if cg == pg:
            print('It\'s a draw! You both picked ' + compsign)
            return 'd'
        else:
            if cg == 1:
                if pg == 2:
                    print('You win! Paper beats rock!')
                    return 'w'
                elif pg == 3:
                    print('You lose! Rock beats scissors!')
                    return 'l'
            elif cg == 2:
                if pg == 1:
                    print('You lose! Paper beats rock!')
                    return 'l'
                elif pg == 3:
                    print('You win! Scissors beats paper!')
                    return 'w'
            else:
                if pg == 1:
                    print('You win! Rock beats scissors!')
                    return 'w'
                elif pg == 2:
                    print('You lose! Scissors beats paper!')
                    return 'l'
    # 向外传递单局结果
    return game(compguess, playguess)


playagain = 'y'
gamesplayed = 0
# 初始化三个计数变量
win_count = 0
lose_count = 0
draw_count = 0


while playagain == 'y' or playagain == 'Y':
    # 接收单局结果
    result = fullgame()
    gamesplayed = gamesplayed + 1
    # 根据结果更新计数
    if result == 'w':
        win_count += 1
    elif result == 'l':
        lose_count +=1
    else:
        draw_count +=1
    # 打印完整统计
    print(f'You have played {gamesplayed} games.')
    print(f'Current record: {win_count} wins, {lose_count} losses, {draw_count} draws.')
    playagain = input('Play again? y/n: ')
    while playagain != 'y' and playagain != 'n':
        print('You must pick y or n!')
        playagain = input('Play again? y/n: ')

print('Thank you for playing rock, paper, scissors! This has been coded in Python by Ethan Lang!')

额外优化:删掉了原代码里多余的一次numberallocator(playsign)重复调用,还修正了原代码里石头对剪刀的胜负判断文案错误(原文案写反了规则)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.01 21:06:36