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

Python Trivia游戏:askQuestion返回True后score未递增问题

Trivia问答游戏得分不增长问题修复

我正在开发一款Trivia问答游戏,原本设定是玩家答对问题时askQuestion函数返回True,调用它的playGame函数会把score加1,但实际运行时score始终没增长。调试发现程序走到了if askQuestion == True这句,却没执行得分递增操作。

问题代码

playGame函数

# play game
def playGame(questions, startTime, score):
    # initialize start time
    startTime = time.time()
    # set remaining time equal to 60 seconds
    remainingTime = AVAILABLE_TIME
    # initialize time remaining
    timeLeft = 100    
    # initialize score to zero
    score = 0
    # initiazlize question number to zero
    questionNumber = 0
    # run loop while time left
    while timeLeft > 0:
        # go into spinner function
        spinner(questions[questionNumber][0], score)
        # go into askQuestion function
        askQuestion(questions[questionNumber], timeLeft, score)
        # update score:
        # if got the correct answer:
        if askQuestion == True:
            # increment score
            score += 1
        # increment questionNumber
        questionNumber += 1
        # calculate remaining time
        remainingTime -= time.time() - startTime + 14
        # update time left
        timeLeft = remainingTime - 50

askQuestion函数

def askQuestion(questions, timeLeft, score):
    # initialize start time
    start = time.time()
    # correct answer number
    ansNumber = questions[6]
    # draw the question board:
    # clear screen
    Draw.clear()
    # set font family
    Draw.setFontFamily('Helvetica')
    # set font size
    Draw.setFontSize(16)
    # draw question box
    Draw.setColor(Draw.BLACK)
    Draw.rect(50, 50, 400, 100)
    # draw the question
    Draw.string(questions[1], 50, 60)
    # set font size for answer
    Draw.setFontSize(20)
    # draw answerBox 0
    answerBox0 = Draw.rect(50, 150, 400, 75)
    # draw answer 0
    Draw.string(questions[2], 50, 160)
    # draw answerBox 1
    answerBox1 = Draw.rect(50, 225, 400, 75)
    # draw answer 1
    Draw.string(questions[3], 50, 235)
    # draw answerBox 2
    answerBox2 = Draw.rect(50, 300, 400, 75)
    # draw answer 2
    Draw.string(questions[4], 50, 310)
    # draw answerBox 3
    answerBox3 = Draw.rect(50, 375, 400, 75)
    # draw answer 3
    Draw.string(questions[5], 50, 385)  
    # draw remainingTime
    Draw.string('Remaining Time: ' + str(int(timeLeft)), 55, 465)             
    
    # while time is remaining:
    while timeLeft > 0:        
        # if the user clicked
        if Draw.mousePressed() == True:
            Draw.clear()
            # get x, y of the click
            x = Draw.mouseX()
            y = Draw.mouseY()
            # if clicked in answerBox0:
            if x > 50 and x < 50 + 400 and y > 150 and y < 150 + 75:
                # if the correct answer number is 0:
                if ansNumber == 0:
                    # set font size
                    Draw.setFontSize(32)
                    # display good message
                    Draw.string('GOOD JOB!', 200, 200)                    
                    return True
                else:
                    # set font size
                    Draw.setFontSize(32)
                    # display negative message
                    Draw.string('Incorrect', 200, 200)
                    return False
            # if clicked in answerBox1:
            elif x > 50 and x < 50 + 400 and y > 225 and y < 225 + 75:
                # if the correct answer number is 1:
                if ansNumber == 1:
                    # set font size
                    Draw.setFontSize(32)
                    # display good message
                    Draw.string('GOOD JOB!', 200, 200)                    
                    return True
                else:
                    # set font size
                    Draw.setFontSize(32)
                    # display negative message
                    Draw.string('Incorrect', 200, 200)                    
                    return False
            # if clicked in answerBox2:
            elif x > 50 and x < 50 + 400 and y > 300 and y < 300 + 75:
                # if the correct answer number is 2:
                if ansNumber == 2:
                    # set font size
                    Draw.setFontSize(32)
                    # display good message
                    Draw.string('GOOD JOB!', 200, 200)                    
                    return True
                else:
                    # set font size
                    Draw.setFontSize(32)
                    # display negative message
                    Draw.string('Incorrect', 200, 200)                    
                    return False
            # if clicked in answerBox3:
            elif x > 50 and x < 50 + 400 and y > 375 and y < 375 + 75:
                # if the correct answer number is 3:
                if ansNumber == 3:
                    # set font size
                    Draw.setFontSize(32)
                    # display good message
                    Draw.string('GOOD JOB!', 200, 200)                    
                    return True
                else:
                    # set font size
                    Draw.setFontSize(32)
                    # display negative message
                    Draw.string('Incorrect', 200, 200)                    
                    return False
            # initialize end time
            end = time.time()
            # update remaining time
            timeLapsed = end - start
            timeLeft -= timeLapsed

问题分析

  1. 核心错误:playGame里调用askQuestion后没有保存返回值,之后判断if askQuestion == True时,askQuestion是函数对象本身,永远不会等于布尔值True,所以得分递增的代码永远不会执行。
  2. 冗余参数赋值:playGame的参数startTime和score被函数内部重新赋值,导致传入的初始值完全失效。
  3. 时间计算逻辑混乱:remainingTime -= time.time() - startTime +14的公式不符合常规时间计算逻辑,timeLeft = remainingTime -50也会导致剩余时间计算错误。
  4. 时间更新缩进错误:askQuestion里的时间更新代码在if Draw.mousePressed()分支内,只有用户点击时才会更新剩余时间,没点击的话时间不会减少。

修复后的代码

playGame函数

# 游戏主逻辑
def playGame(questions):
    startTime = time.time()
    remainingTime = AVAILABLE_TIME  # 假设AVAILABLE_TIME是预设的总时长,比如60秒
    timeLeft = remainingTime
    score = 0
    questionNumber = 0
    
    # 同时限制剩余时间和题目数量,避免数组越界
    while timeLeft > 0 and questionNumber < len(questions):
        spinner(questions[questionNumber][0], score)
        # 保存askQuestion的返回值
        is_correct = askQuestion(questions[questionNumber], timeLeft, score)
        # 判断返回值是否为True,正确则加分
        if is_correct:
            score += 1
        
        questionNumber += 1
        # 修正时间计算:直接计算已流逝时间,剩余时间=总时长-已流逝时间
        elapsed_time = time.time() - startTime
        timeLeft = remainingTime - elapsed_time

askQuestion函数

def askQuestion(questions, timeLeft, score):
    start = time.time()
    ansNumber = questions[6]
    
    # 绘制界面
    Draw.clear()
    Draw.setFontFamily('Helvetica')
    Draw.setFontSize(16)
    Draw.setColor(Draw.BLACK)
    Draw.rect(50, 50, 400, 100)
    Draw.string(questions[1], 50, 60)
    Draw.setFontSize(20)
    
    # 绘制选项框与选项文本
    Draw.rect(50, 150, 400, 75)
    Draw.string(questions[2], 50, 160)
    Draw.rect(50, 225, 400, 75)
    Draw.string(questions[3], 50, 235)
    Draw.rect(50, 300, 400, 75)
    Draw.string(questions[4], 50, 310)
    Draw.rect(50, 375, 400, 75)
    Draw.string(questions[5], 50, 385)
    
    Draw.string('剩余时间: ' + str(int(timeLeft)), 55, 465)             
    
    while timeLeft > 0:        
        # 每次循环都更新剩余时间,确保倒计时正常
        elapsed = time.time() - start
        timeLeft -= elapsed
        start = time.time()  # 重置起始时间,保证下次计算准确
        
        if Draw.mousePressed():
            Draw.clear()
            x = Draw.mouseX()
            y = Draw.mouseY()
            
            # 判断点击区域,简化数值计算
            if 50 < x < 450 and 150 < y < 225:
                if ansNumber == 0:
                    Draw.setFontSize(32)
                    Draw.string('答对了!', 200, 200)                    
                    return True
                else:
                    Draw.setFontSize(32)
                    Draw.string('答错了', 200, 200)
                    return False
            elif 50 < x < 450 and 225 < y < 300:
                if ansNumber == 1:
                    Draw.setFontSize(32)
                    Draw.string('答对了!', 200, 200)                    
                    return True
                else:
                    Draw.setFontSize(32)
                    Draw.string('答错了', 200, 200)                    
                    return False
            elif 50 < x < 450 and 300 < y < 375:
                if ansNumber == 2:
                    Draw.setFontSize(32)
                    Draw.string('答对了!', 200, 200)                    
                    return True
                else:
                    Draw.setFontSize(32)
                    Draw.string('答错了', 200, 200)                    
                    return False
            elif 50 < x < 450 and 375 < y < 450:
                if ansNumber == 3:
                    Draw.setFontSize(32)
                    Draw.string('答对了!', 200, 200)                    
                    return True
                else:
                    Draw.setFontSize(32)
                    Draw.string('答错了', 200, 200)                    
                    return False

修复要点总结

  • 保存askQuestion的返回值到变量,通过判断变量值来执行得分递增操作。
  • 移除playGame中冗余的参数赋值,添加题目数量判断防止数组越界。
  • 修正时间计算逻辑,让剩余时间的更新符合实际流逝时长。
  • 调整askQuestion中时间更新代码的缩进,确保每帧都更新剩余时间。
  • 简化点击区域的判断逻辑,让代码更易读。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 22:10:30