类错误缩进?但是哪里?

时间:2014-11-17 14:08:11

标签: python python-2.7

这是我的产品类

class Product(object):

    def __init__(self,price,name,catalognum,starRating):
        self.price = price
        self.name = name
        self.catalognum = catalognum
        self.starRating = starRating

    def __str__():
        print "Catalognum:[0]\n\
            Name: [1]\n\
            Price: $[2]\n\
            Rating: [3]".format(catalognum,name,price,num_starRating)

    def num_starRating(self):
        return "*"*int(self.rating)

class Book(object):

    def __init__(self,price,name,catalognum,starRating,author,ISBN,publisher):
    self.price = price
    self.name = name
    self.catalognum = catalognum
    self.starRating = starRating
    self.author = author
    self.ISBN = ISBN
    self.publisher = publisher

    def __str__():
    print "author:[0]\n\
            Title:[1]\n\
            Price:$[2]\n\
            ISBN:[3]\n\
            Publisher:[4]\n\
            Rating[5]".format(author,Title,Price,ISBN,num_starRating)

class Movie(object):

    def __init__(self,Director,Studio,Title,Price,Running_Time,starRating,name,catalognum):
    self.Director = Director
    self.Studio = Studio
    self.Title = Title
    self.Price = Price
    self.Running_Time = Running_Time
    self.starRating = starRating
    self.name = name
    self.catalognum = catalognum


    def __str__():
    print "Director:[0]\n\
            Title:[1]\n\
            Price:[2]\n\
            Running_Time:[3]min\n\
            Studio:[4]\n\
            Rating[5]".format(Director,Title,Price,Running_Time,Studio,num_starRating)

这是我的目录类

class Catalog(object):

    def __init__(self,product_file):
        self.product_file = product_file
        sortByColumn = 0
        self.cataloglist = self.BuildCatalogList

def BuildCatalogList(self):
    file = open(self.product_file,"r")
    filelist = file.readlines()
    product_list = []
    for i in range(1,len(file_list)):
        product = filelist[i].split(',')
        item = product(float(product[2]),int(product[3]),product[1],product[0])
        product_list.append(item)
        return product_list

def setsortby(self,sortype):
    self.sortbycolumn = sorttype

def printcatalogtable(self):
    print “[0][1][2][3]”.format(“catalog #”,”name”,”price($)”,”rating”)

import os
directory = os.listdir(“.”) 
for filename in directory:
if filename [:-3:] == “pyc”:
os.remove(filename)
catalog = catalog(“bookdata.txt)


for printcatalogtable

print”[0:20]][1:25][2:15][3]”.format(“catalog #”,”name”,”price($)”,”rating)
for product in cataloglist:
print “(0:20)(1:25)(2:15.2f)       (3)”[3]”.format(product,catalognum,product,name,price,name,product,price,product.getstarrating())

使用两个类,我试图打印出我的文件,我已经保存在目录类之后的格式中 但它没有成功,任何想法?

它应该像这样:

Catalog Number (6 digits),Title,Price,Star Rating (1-5),Author First Name,Author Last Name,ISBN,Publisher
123456,Game of Thrones: A Song of Ice and Fire,11.99,5,George RR,Martin,9780553582017,Random House Publishing Group
654321,City of Bones,11.99,4,Cassandra,Clare,9781406331400,Margaret K McElderry Books
654613,How I Met My Husband,14.99,4,Alice,Munro,2354365435123,Book Bublishers Inc
524638,The Hunger Games,9.99,4,Susan,Collins,9780439023481,Scholastic Press
632356,Lives of the Saints,19.99,2,Ninno,Ricci,8883336666,Harol Hitch Hijackers Books
675031,1984,11.99,5,George,Orwell,1782127755,Secker and Warburg London
111111,Forbidden City,5.99,1,William,Bell,4435-13422453,Lamest Books Corp
315644,Harry Potter and the Prisoner of Azkaban,14.99,5,JK,Rowling,64569-7861-0537,Raincoast
478931,Fifty Shades of Grey,2.99,0,EL,James,783844-6512-982,BooksBooksBooks Inc.
101010,Breaking Dawn,0.99,1,Stephanie,Meyer,101010-1010-101,LOLOLOLOL Press
548573,The Great Gatsby,14.99,4,F Scott,Fitzgerald,9781597226769,Scribners
123827,Steve Jobs,39.99,4.5,Walter,Isaacson,9781451648539,Google Inc
453123,Twilight,0.1,1,Stephenie,Meyer,9781594133299,Simons Inc.
445234,A Midsummer Night's Dream,10.99,3,William,Shakespeare,123455-4322-144,Penguin Group
542324,Paper Town,12.99,2,John,Green,698773-3122-341,Penguin Group
991337,Shutter Island,19.99,4.5,Dennis,Lehane,1234567890154,Awesome Group
123431,The Magic School Bus at the Waterworks,50,5,Joanna ,Cole,0-590-40360-5,Scholastic Corporation

2 个答案:

答案 0 :(得分:1)

你的BuildCatalogList从0开始,这应该改变如下(当然还有属于类目录的所有其他函数。

class Catalog(object):

    def __init__(self,product_file):
        self.product_file = product_file
        sortByColumn = 0
        self.cataloglist = self.BuildCatalogList

    # Spaces added to this function.
    def BuildCatalogList(self):
        file = open(self.product_file,"r")
        filelist = file.readlines()
        product_list = []

        for i in range(1,len(file_list)):
            product = filelist[i].split(',')
            item = product(float(product[2]),int(product[3]),product[1],product[0])
            product_list.append(item)

        return product_list

答案 1 :(得分:1)

Book和Movie类的__init__和__str__方法的缩进是错误的。缩进它。