从函数外部调用函数时不返回值?

时间:2017-01-30 12:22:45

标签: python python-3.x user-defined-functions

我正在调用该函数,而a并未保留以下函数中的值mob_f return

a = get_mobile()

功能是:

def get_mobile():
    ws = sheet_select()
    mobile = []
    mob_i = []
    mob_f = []
    mob_j = []

    for col in ws.iter_cols():
        for cell in col:
            if cell.value == 'Mobile':
                x=column_index_from_string(cell.column)
                for row in ws.iter_rows(min_col = x, min_row = 2, max_col = x):
                    for cell in row:
                        if cell.value != None:
                            mobile.append(cell.value)
    for i in mobile:
        h = i
        h = h.replace(" ", "")
        h = h.replace("+(91)-", ",")
        h = h.replace("+91", "")
        h = h.replace("-", "")
        mob_i.append(h)

    for i in mob_i:
        h = i
        h = h.split(',')
        mob_j.append(h)
    mob_x = [item for sublist in mob_j for item in sublist]
    for i in mob_x:
        if i != '':
            h = i
            return mob_f.append(h)

如果我使用代码时没有将其定义为function,那么它运行没有问题,我得到mob_f

我认为问题是return放错了。我尝试了很多组合并且一直都在失败。也是昨晚同样的功能正在运作,我无法理解我哪里出错了。

1 个答案:

答案 0 :(得分:1)

Append什么都不返回,我想你想要这个:

...
for i in mob_x:
    if i != '':
        h = i
        mob_f.append(h)
return mob_f