-2

I am new to Python. I want to call a function that detect null value in a excel table and passing "Fail" to a variable 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')

Above code will return "Fail" but below code will give: NameError: name 'stat' is not defined

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

How can I assign "Fail" to stat if the function find the empty cell?

Yun
  • 1
  • 4
  • you should `return s` so the line at the bottom is `print checkBlank('Sheet1')` – Dylan Lawrence Dec 07 '17 at 16:35
  • 1
    Well, where _is_ `stat` defined? I can't see it in your code. Also, your function takes only one parameter. – tobias_k Dec 07 '17 at 16:36
  • correction: def checkBlank(tb, stat): – Yun Dec 07 '17 at 16:37
  • 1
    please correct it in your description not in comment thanks. and your error should not be stat is not define once you update your function. please also post the new error as well if you are getting any. – stucash Dec 07 '17 at 16:37
  • 1
    Related, possibly duplicate: [How do I pass a variable by reference?](https://stackoverflow.com/q/986006/1639625) – tobias_k Dec 07 '17 at 16:38
  • 1
    You probably want `stat = checkBlank("sheet1"); print stat`. Also _please_ return a boolean `True` or `False` instead of some arbitrary string. – tobias_k Dec 07 '17 at 16:39
  • stat = checkBlank("Sheet1"); print stat returns "None" – Yun Dec 07 '17 at 16:45
  • 1
    Given that the first snippet works correctly, what is the point of your question? No, you can't do this in Python, but you don't need to. – Daniel Roseman Dec 07 '17 at 16:48
  • Please see [Other languages have "variables", Python has "names"](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables). For a more in-depth discussion, please see [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder. – PM 2Ring Dec 07 '17 at 16:54

1 Answers1

1

It appears that you are attemping to pass a variable into a function to be used as the return value. This is not Pythonic and in general will not work. Instead, your function should be returning a value and you should be assigning that value to a new "variable."

def function(arg):
    if arg:
        return 'Pass'
    else:
        return 'Fail'

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

In Python, you don't want to try to write a function that calls by reference, because variables don't exist in Python in the same way that they do in C. Instead, what we have are names which are more akin to placeholders and can be used to retrieve objects. This article and many other Stack Overflow answers go into this in more depth.

Jared Goguen
  • 8,772
  • 2
  • 18
  • 36
  • Thank you!! I need to apply same function to 20 tables, and another 2 functions to some of the tables, so stat1 ....... stat20 changes each time. Just wanted to make the code simpler. It seems kind of inconvenience to me if I cannot do same thing as in C.... – Yun Dec 07 '17 at 17:29
  • @Yun You might want to think about using a dictionary or a list! `stat[1] = function1()`, `stat[2] = function2()`, etc... – Jared Goguen Dec 07 '17 at 17:50
  • Awesome! will try. – Yun Dec 07 '17 at 17:57