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

Python中终端打印RAM地址而非实际问题文本的解决求助

解决Python打印对象显示内存地址的问题

问题原因

你在QuizBrain的next_question方法中直接输出了Question类的实例对象,Python默认会打印对象的内存地址而非你期望的问题文本;同时代码缺少调用check_answer验证答案的逻辑,导致分数无法更新。

修复方案

方案1:直接访问Question对象的text属性(最直接)

修改quiz_brain.py中的next_question方法,将current_question替换为current_question.text,同时补充调用check_answer的逻辑:

def next_question(self):
    current_question = self.question_list[self.question_number]
    self.question_number += 1
    user_answer = input(f"{self.question_number}: {current_question.text} (True/False): ")
    # 调用check_answer验证答案并更新分数
    self.check_answer(user_answer, current_question.answer)

方案2:给Question类添加__str__方法(更优雅)

在question_model.py的Question类中添加__str__方法,让Python打印对象时自动返回问题文本:

class Question:
    def __init__(self, text, answer):
        self.text = text
        self.answer = answer
    
    def __str__(self):
        return self.text

同时补充next_question中调用check_answer的逻辑:

def next_question(self):
    current_question = self.question_list[self.question_number]
    self.question_number += 1
    user_answer = input(f"{self.question_number}: {current_question} (True/False): ")
    self.check_answer(user_answer, current_question.answer)

验证效果

修改后运行main.py,终端会正常显示问题文本,每道题回答后会输出对错提示与当前分数,最终展示总成绩。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 00:40:57