在python函数中返回变量时出错

2024-04-25 14:41:25 发布

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

我只是试图修改一个字符串并返回修改后的字符串,但是在打印变量时返回“None”。你知道吗

def AddToListTwo(self,IndexPosition):
    filename = RemoveLeadingNums(self, str(self.listbox1.get(IndexPosition))) #get the filename, remove the leading numbers if there are any
    print filename #this prints None
    List2Contents = self.listbox2.get(0, END)
    if(filename not in List2Contents): #make sure the file isn't already in list 2
        self.listbox2.insert(0, filename)

def RemoveLeadingNums(self, words):
    if(isinstance(words,str)):
        match = re.search(r'^[0-9]*[.]',words)
        if match: #if there is a match, remove it, send it through to make sure there aren't repeating numbers
            RemoveLeadingNums(self, re.sub(r'^[0-9]*[.]',"",str(words)).lstrip())
        else:
            print words #this prints the value correctly
            return words
    if(isinstance(words,list)):
        print "list"

编辑-多人评论说,如果存在匹配项,我不会返回值。如果有的话,我不想退货。它可能是重复的(例如:1.2。项目a)。所以,我想基本上使用递归来删除它,然后返回值


Tags: the字符串selfnonegetifdefmatch
2条回答

如果是火柴你什么也不退。应该是:

return RemoveLeadingNums( ... )

有多个条件RemoveLeadingNums返回None。e、 g.如果采用if match:分支。也许应该是:

if match: 
    return RemoveLeadingNums(...

如果有任何数据类型不是传入的字符串,则还返回None。你知道吗

相关问题 更多 >