为什么我的函数无法从同一个类的另一个函数中获取值

2024-10-02 10:33:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从类的另一个函数中获取列表,但是这个函数返回空列表。我不明白如何连接这个函数而不丢失列表数据(

from classCR import *
...

if Main == 1:
        LenArray.InputArr(a)

    if Main == 2:
        LenArray.RandomArr(a)   

    if Main == 3:
        LenArray.GetItemArr(1)

和等级CR

class ClassArray:

    def __init__(self,LenghtArray):
        self.LenghtArray = LenghtArray
        self.ArrayInpList = []
        self.ArrayRandList = []


    def InputArr(self,LenghtArray):

        for i in range(self.LenghtArray):
            self.ArrayInpList.append(int(input('Input value of '+str(i)+': ')))
        print(self.ArrayInpList)

    def RandomArr(self,LenghtArray):
        for i in range(self.LenghtArray):
            self.ArrayRandList.append(random.randint(-30,30))
        print(self.ArrayRandList)



    def GetItemArr(self):
        print(self.ArrayRandList)

在def GetItemArr中,我想从def RandomArr获取list的值。但是当我在主代码LenArray.GetItemArr()中更改为条件2时-它可以工作…enter image description here


Tags: 函数self列表forifmaindefprint

热门问题