Python 将函数结果存储到变量中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32364127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:26:06  来源:igfitidea点击:

Store function result into variable

pythonpython-3.x

提问by storks

I am trying to save the result of a function into a variable and print that variable on the screen, but when I print I see "none".
How to repair this?

我试图将函数的结果保存到一个变量中并在屏幕上打印该变量,但是当我打印时我看到“无”。
这个怎么修?

import time;


def hours():
    localtime =  time.localtime(time.time())
    print (localtime.tm_hour)

def minutes():
    localtime =  time.localtime(time.time()) 
    print (localtime.tm_min)

def seconds():
   localtime =  time.localtime(time.time())     
   print (localtime.tm_sec)


hours()
minutes()
seconds()

var = hours()
print(var)

采纳答案by vmonteco

You need to return a value that will be stored into the variable.

您需要返回一个将存储到变量中的值。

This way :

这边走 :

def myfunction():
    value = "myvalue"
    return value

var = myfunction()
print(var)

>>> "myvalue"

Currently you're just printing the value in your function, not returning it , that's two different things.

目前你只是在你的函数中打印值,而不是返回它,这是两件不同的事情。

Edit:Also note that the default returned value is Nonewhen there is no return directive.

编辑:另请注意,默认返回值None是没有返回指令时。

回答by Harpal

You need to return localtime.tm_hournot print

你需要返回localtime.tm_hourprint

def hours():
    localtime =  time.localtime(time.time())
    print (localtime.tm_hour)
    return localtime.tm_hour

回答by Vincent Murphy

You are not returning the value. The script should do:

您没有返回值。脚本应该这样做:

import time;


def hours():
    localtime =  time.localtime(time.time())
    print (localtime.tm_hour)
    return localtime.tm_hour

In [11]: var = hours()
18
In [12]: print(var)
18

see https://docs.python.org/3.5/tutorial/controlflow.html#defining-functionsfor how to use return.

有关如何使用 return 的信息,请参阅https://docs.python.org/3.5/tutorial/controlflow.html#defining-functions

回答by Mansur Ali

I able to make this working with return within function definition

我能够在函数定义中使用 return 进行此操作

def hourMin ():
    localtime = time.localtime(time.time())
    return '{0}{1}{2}'.format(localtime.tm_hour, localtime.tm_min, localtime.tm_sec)

def dateDay ():
    localtime = time.localtime(time.time())
    return '{0}{1}{2}'.format(localtime.tm_mday, localtime.tm_mon, localtime.tm_year)

Calling area

通话区

def rannum ():
    for x in range(1):
        valo = random.randint(1,21)*5

    TimeHour = hourMin()
    TimeDate = dateDay()
    print ('Time is ' + str(hourMin()))
    path = "/tmp/temp/"
    curr = path + str(valo) + str(TimeDate)
    try:
        os.mkdir(curr)
    except:
        print('Creation of the directory %s failed' % path)
    else:
        print('Directory %s created ' % curr)

rannum()