Home > Blockchain >  Get specific value from function
Get specific value from function

Time:11-16

i was trying to do this:

def get_sum_col(col):
    val = ""
    cont = ""
    for x in range(1, mxrw1):
        if type(ws_Sheet1.cell(row=x, column=col).value) == int or type(
                ws_Sheet1.cell(row=x, column=col).value) == float:
            if val == "":
                cont = 1
                val = ws_Sheet1.cell(row=x, column=col).value
            else:
                cont  = 1
                val = int(val)   int(ws_Sheet1.cell(row=x, column=col).value)
    media = int(val) / int(cont)
    return val, media

And then to get the value i need something like

print(get_sum_col(3).media
print(get_sum_col(3).val

is this possible? what am i doing wrong?

CodePudding user response:

val, media = get_sum_col(3)

print(val)

print(media)

Or you can make get_sum_col an object.

CodePudding user response:

Returning two variables doesn't return an object: it's returning a tuple. val, media is shorthand for (val, media). You can just turn the tuple back into two variables like so:

def get_sum_col(col);
    ...
    return val, media
...
val, media = get_sum_col(col)

CodePudding user response:

Yes, you can do something like this:

def get_sum_col(col):
    ...
    return {'val': val, 'media': media}

print(get_sum_col(3).get('media', None) # None will print if media is bull

CodePudding user response:

val, media = get_sum_col(3) will only call your function once but will initialize the val and media variables to the return value from the function.

by the way - you can call those variables in any name in your caller code.

  • Related