如何将函数的结果存储在Python中的变量中? [英] How can I store a result of a function in a variable in Python?

查看:343
本文介绍了如何将函数的结果存储在Python中的变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将 random.random 的结果存储到变量中,如下所示:

<$ p $ ()








$ rand $ ()

我该如何正确得到?



谢谢。

解决方案

通常,您返回值。一旦你做完了,你可以像返回任何东西一样为返回值赋一个名字:

  def randombb(): 
return random.random()

a = randombb()

作为一个方面说明 - python教程对于那些想了解python基础知识的人来说非常棒。我强烈建议你检查一下。以下是功能部分: https://docs.python.org/ 2 / tutorial / controlflow.html#定义功能 ...


I'm trying to store the result of random.random into a variable like so:

import  random

def randombb():
 a = random.random()
    print(a)

randombb()

How can I properly get a?

Thanks.

解决方案

Generally, you return the value. Once you've done that, you can assign a name to the return value just like anything else:

def randombb():
    return random.random()

a = randombb()

As a side note -- The python tutorial is really great for anyone looking to learn a thing or two about the basics of python. I'd highly recommend you check it out. Here is the section on functions: https://docs.python.org/2/tutorial/controlflow.html#defining-functions ...

这篇关于如何将函数的结果存储在Python中的变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆