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

Python中打印两个自定义对象无输出问题求助

问题排查:Python类代码无输出问题

问题描述

我编写了vehicle(汽车)和shoe(鞋子)两个类的Python代码,期望实现类似如下示例的格式打印两个对象:

Cat 0. Cat 1. Cat
Dog 1. Dog 2. Dog
Horse 2. Horse 3. Horse
但代码运行后无任何输出,也没有报错,希望得到帮助排查问题。

原始代码

#object 1 is car 
class vechicle:

    car=0
    color="Blue"
    model="Ford"
    size="4 Door"

#properties

def __init__(self, model, color, size):
 
    self.model = model
    self.car = 0
    self.color = color
    self.size = size
    
def cars_sold(self):
    self.car = self.car+1
    return self.car

def see_color(self):
    return self.color

def size(self):
    return self.size

def model(self):
    return self.model

def set_color(self, new_color= 'Blue'):
    self.color = new_color

#object 2


class shoe:
    
#creating a list
    
    list=[]
    list_name = "Shoes"
    list_decription = "Records of types of shoes"
    list_owner = "Vans"

 #properties   
    
    def __init__(self, name, description, owner):
        self.list_name = name
        self.list_description = decription
        self.list_owner = owner
        
        def item(self,item):
            self.list.append(item)
            
        def length(self):
            return len(self.list)
        
        def decription(self):
            return self.list_decription
        def owner(self):
            return self.list_owner
        def change_owner(self, new):
            self.list_owner = new
            
#sending the values

    def printing(self,index = None):
        if index==0:
            i=0
            while i < len(self.list):
                print(i+1,".",self.list[i])
                i = i +1
            if index ==1:
                    i=0
                    while i<len(self.list):
                        print(self.list[i])
                        i=i+1
                        print(object)

问题原因及修复方案

1. 根本原因:未实例化类、未调用方法

代码仅定义了类结构,但没有创建任何类的实例,也没有调用printing等输出相关方法,程序执行后自然没有任何输出。

2. 类方法缩进错误

  • vechicle类的所有方法(__init__、cars_sold等)都缩进到了类定义外部,不属于类的成员,导致类没有可用的实例方法。
  • shoe类里的item、length等方法都嵌套在__init__方法内部,变成了构造函数的局部函数,外部无法调用。

修复:将类的方法缩进至类定义内部,shoe的方法要和__init__同级。

3. 拼写错误

  • 类名vechicle拼写错误,正确应为Vehicle(Python约定类名大驼峰)。
  • shoe类__init__方法中,decription应为description,导致变量赋值失败。

4. printing方法逻辑错误

  • 嵌套if index ==1缩进错误,它被包含在if index==0的代码块内,永远无法触发。
  • print(object)是打印object类本身,并非实例内容,逻辑错误。

5. 类属性与实例属性混淆

shoe类的list是类属性,所有实例会共享同一个列表,多实例操作时会导致数据混乱,应改为实例属性,在__init__中初始化。

修正后的示例代码

# 汽车类(修正拼写和缩进)
class Vehicle:
    def __init__(self, model, color, size):
        self.model = model
        self.car_count = 0  # 改名避免与类名混淆
        self.color = color
        self.size = size
    
    def cars_sold(self):
        self.car_count += 1
        return self.car_count
    
    def see_color(self):
        return self.color
    
    def get_size(self):  # 改名避免与属性size重名
        return self.size
    
    def get_model(self):  # 改名避免与属性model重名
        return self.model
    
    def set_color(self, new_color='Blue'):
        self.color = new_color

# 鞋子类(修正缩进、拼写、属性逻辑)
class Shoe:
    def __init__(self, name, description, owner):
        self.list_name = name
        self.list_description = description
        self.list_owner = owner
        self.item_list = []  # 改为实例属性,避免多实例共享
    
    def add_item(self, item):
        self.item_list.append(item)
    
    def get_length(self):
        return len(self.item_list)
    
    def get_description(self):
        return self.list_description
    
    def get_owner(self):
        return self.list_owner
    
    def change_owner(self, new_owner):
        self.list_owner = new_owner
    
    def printing(self, index=None):
        if index == 0:
            for i, item in enumerate(self.item_list):
                print(f"{i+1}. {item}")
        elif index == 1:
            for item in self.item_list:
                print(item)
        # 默认打印你想要的格式
        else:
            for i, item in enumerate(self.item_list):
                print(f"{item}         {i}.  {item}         {i+1}.  {item}")

# 实例化并调用方法,产生输出
if __name__ == "__main__":
    # 创建鞋子实例并添加内容
    shoe_instance = Shoe("Shoes", "Records of types of shoes", "Vans")
    shoe_instance.add_item("Cat")
    shoe_instance.add_item("Dog")
    shoe_instance.add_item("Horse")
    # 调用打印方法,输出目标格式
    shoe_instance.printing()
    
    # 测试汽车类
    car1 = Vehicle("Ford", "Blue", "4 Door")
    print(f"汽车信息:{car1.get_model()} | {car1.see_color()} | {car1.get_size()}")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 01:55:21