【问题标题】:IndexError: list assignment index out of range [duplicate]IndexError:列表分配索引超出范围[重复]
【发布时间】:2012-06-23 16:22:04
【问题描述】:
class Packagings:
    def __init__(self):
        self.length=0
        self.deckle=0
        self.tmp=0
        self.flute=[]
        self.gsm=[]
        self.t_weight=0
        self.weight=0

    def read_values(self):
        print """Select Type Of Paper
               1.3 Ply
               2.5 Ply
               3.7 Ply
               """
        self.type=input("Enter the type:")
        self.l=input("Enter Length:")
        self.b=input("Enter Breadth:")
        self.h=input("Enter Height:")
        self.flap=input("Enter Flap:")


    def ply_read_values(self):
        for i in range(0,2):
            self.flute[i]=input("Enter flute:")
            self.gsm[i]=input("Enter Gsm:")
            self.weight[i]=self.tmp*(flute[i]*gsm[i])
            self.t_weight=self.t_weight+self.weight[i]

    def do_calc(self):
        self.length=(2*self.l+2*self.b+self.flap)/1000
        self.deckle=(float(self.h+self.b))/1000
        self.tmp=self.length*self.deckle


    def print_value(self):
        print self.length
        print self.deckle
        print self.t_weight

#Main Function
obj=Packagings()
obj.read_values()
obj.do_calc()
obj.ply_read_values()
obj.print_value()

当我运行这个程序时,我得到以下错误。我找不到列表分配如何超出范围。你们能通过我的程序告诉我哪里出错了吗?对我来说,这份清单似乎很好。它是如何超出索引的?

Traceback (most recent call last):
  File "C:/Python27/packagings.py", line 47, in <module>
    obj.ply_read_values()
  File "C:/Python27/packagings.py", line 27, in ply_read_values
    self.flute[i]=input("Enter flute:")
IndexError: list assignment index out of range

【问题讨论】:

    标签: python


    【解决方案1】:

    self.flute 被初始化为一个空列表,因此它没有可以重置的元素。使用self.flute.append 并类似地用于您的其他列表。

    【讨论】:

    • 感谢 maan 的帮助
    【解决方案2】:

    假设您的代码,您需要将self.weight=0 替换为self.weight=[] 我觉得是你的问题

    【讨论】:

    • 是的,这是我没有注意到的另一个错误...感谢亲爱的帮助我解决它
    猜你喜欢
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 2018-10-21
    • 2020-02-13
    • 1970-01-01
    • 2019-03-28
    相关资源
    最近更新 更多