不使用上面函数的变量的函数

2024-04-26 05:40:49 发布

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

我有一个程序,其中定义了一个名为historyA()的函数

def historyA(self, name, price, category, sub, comment):
    sh = login("Budget")
    worksheet = sh.get_worksheet(0)
    emptyrowresize()
    date = getDate()
    toAppend = [date, name, category, sub, price, comment]
    worksheet.append_row(toAppend)

在这个函数中,它给出变量的值。你知道吗

紧接着它运行函数emptyrowresize()

def emptyrowresize():
    print("\n")
    count = 0
    cycle = 0
    empty = False

    for i in range(emptyrowcount()):
        for i in range(worksheet.col_count):
            if(lastvalue(i+1) == ""):
                count += 1
            if(count >= worksheet.col_count):
                empty = True
            if(empty == True):
                worksheet.resize(worksheet.row_count-1,worksheet.col_count)
                print("Fixing empty row at {0}..." .format(worksheet.row_count+1))
                break

emptyrowresize()调用其中的函数lastvalue()

def lastvalue(x, y=0):
    cycles = 0
    #returns last value in col x with vertical offset y
    while(True):
        count = worksheet.row_count
        val = worksheet.cell(int(count-y), x).value
        cycles += 1
        if(val == ''):
            y = cycles
        else:
            break
    return val

最后一个值需要在historyA()中命名的工作表,但它不断给出错误,即工作表不是已定义的全局变量。你知道吗

为什么这样不行?我曾经有过一次这样的问题,但我不记得我是如何修复它的,而不是为我需要处理的三个工作表中的每一个单独的函数。你知道吗

编辑:我觉得值得一提的是,传递的所有内容historyA()都在我要导入的单独文件中。我不确定这是否意味着什么。你知道吗


Tags: 函数intrueifdefcountcolval