Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python函数参数:未定义NameError参数_Python - Fatal编程技术网

Python函数参数:未定义NameError参数

Python函数参数:未定义NameError参数,python,Python,我是Python新手。我想调用一个函数来检测excel表中的空值,并将“Fail”传递给变量stat def checkBlank(tb): book=xlrd.open_workbook(reportFile) sheet = book.sheet_by_name(tb) s= "Pass" for i in range(sheet.nrows): row = sheet.row_values(i) for cell in r

我是Python新手。我想调用一个函数来检测excel表中的空值,并将“Fail”传递给变量stat

def checkBlank(tb):
    book=xlrd.open_workbook(reportFile)
    sheet = book.sheet_by_name(tb)
    s= "Pass"
    for i in range(sheet.nrows):
        row = sheet.row_values(i)  
        for cell in row:                
            if  cell =='':
                s="Fail"
    return s
print checkBlank('Sheet1')
上述代码将返回“Fail” 但下面的代码将给出:NameError:未定义名称“stat”

def checkBlank(tb,stat):
    book=xlrd.open_workbook(reportFile)
    sheet = book.sheet_by_name(tb)
    s= "Pass"
    for i in range(sheet.nrows):
        row = sheet.row_values(i)  
        for cell in row:                
            if  cell =='':
                s="Fail"
print checkBlank('Sheet1', stat)
print stat

如果函数找到空单元格,如何将“Fail”赋值给stat

似乎您正试图将变量传递到函数中用作返回值。这不是Pythonic,通常不会起作用。相反,函数应该返回一个值,并将该值赋给一个新的“变量”


在Python中,您不想尝试编写通过引用调用的函数,因为Python中的变量与C中的变量不同。相反,我们拥有的是更类似于占位符的名称,可以用来检索对象。还有许多其他堆栈溢出的答案更深入地探讨了这一点。

那么,
stat
是在哪里定义的呢?我在你的代码里看不到。此外,您的函数只接受一个参数。请在说明中更正,不要在注释中更正。谢谢。更新函数后,错误不应该是stat,也不应该是define。如果您遇到任何与.Related相关的、可能重复的错误,也请发布新错误:您可能需要
stat=checkBlank(“sheet1”);打印统计数据
。另外,请返回布尔值
True
False
,而不是任意字符串。鉴于第一个代码段工作正常,您的问题是什么?不,您不能在Python中执行此操作,但您不需要。谢谢!!我需要对20个表应用相同的函数,对一些表应用另外2个函数,所以stat1。。。。。。。stat20每次都会改变。只是想让代码更简单。如果我不能做和C一样的事情,对我来说似乎有点不便…@Yun你可能想用字典或列表
stat[1]=function1()
stat[2]=function2()
,等等……太棒了!我会努力的。
def function(arg):
    if arg:
        return 'Pass'
    else:
        return 'Fail'

status = function(False)
print(status) # 'Fail'