python : no output or only an empty list was produced

不问归期 提交于 2021-02-16 20:08:06

问题


index1 = 0
singlechar = []
def SINGLE_CHAR_VAR(filename):
    firdict = vars_indents(filename)[0]
    firtup_keys = firdict.keys()
    firtup_val = firdict.values()
    for keys in firtup_keys:
        for values in firtup_val:
            index = 0
            for index in range(len(values)):
                firvallist = firtup_val[index]
                for item in firvallist:
                    if len(item[0]) == 1:
                        singlechar.append({'ERROR_TYPE': 'SINGLE_CHAR_VAR', 'LINE_NUMBER': str(keys),'COLUMN': str(item[1]),'INFO': str(item[0]),'SOURCE_LINE': str(lines[keys - 1])})
                    else:
                        continue
                return singlechar

this is my code but there is no output produced or when i move around the return statement an empty list was produced. i was hoping it to give me a list of dictionaries as the output.

can somebody teach me how to fix this?

thank you


回答1:


You should call the function at first to get the output.

SINGLE_CHAR_VAR(filename)

How can you expect a function to run without being called?




回答2:


return != print. You need to do `print SINGLE_CHAR_VAR(filename)

return will send the value to whatever is calling that function. In this case the value/string is being returned, but you need to do something with it, hence the need for print. Alternatively, you can replace return with print if you want the function itself to print the output. However, in this case the value will not be passed along, and you won't be able to store it. It really just comes down to what works for you/what you want. `




回答3:


I think the problem is in the return statement. You should indent the return statement right below the outer for loop but not below the inner for loop. This is shown below:

def SINGLE_CHAR_VAR(filename):
    firdict = vars_indents(filename)[0]
    firtup_keys = firdict.keys()
    firtup_val = firdict.values()
    for keys in firtup_keys:
        for values in firtup_val:
            index = 0
            for index in range(len(values)):
                firvallist = firtup_val[index]
                for item in firvallist:
                    if len(item[0]) == 1:
                        singlechar.append({'ERROR_TYPE': 'SINGLE_CHAR_VAR', 'LINE_NUMBER': str(keys),'COLUMN': str(item[1]),'INFO': str(item[0]),'SOURCE_LINE': str(lines[keys - 1])})
                    else:
                        continue
    return singlechar # indent of return changed


来源:https://stackoverflow.com/questions/26541954/python-no-output-or-only-an-empty-list-was-produced

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!