【问题标题】:Return variable from python module从 python 模块返回变量
【发布时间】:2020-06-19 02:15:59
【问题描述】:

我不知道如何在我导入模块的主程序中获取变量 a 和 b。

scipt1.py:

def med():

    import math
    n=int(input("n: \n"))
    x=float(input("media: \n"))
    s2=float(input("s2: \n"))
    t=float(input("t_{a/2, n-1}: \n"))

    a=x-(t*math.sqrt(s2/n))

    b=x+(t*math.sqrt(s2/n))
    return a,b

在 scipt2.py 中:

from script1 import med

med()

#for example I want to do
x = a - 1000
y = b + 1000

但我得到:"NameError: name 'a' is not defined" 虽然我可以输入变量 n,x,t,s2 的值。 我做错了什么?

【问题讨论】:

  • return 不是这样工作的……你不返回变量你返回一个对象
  • 学习python。阅读教程。

标签: python python-3.x python-module


【解决方案1】:
from script1 import med

a,b = med() #store the values that your function returns

#for example I want to do
x = a - 1000
y = b + 1000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    相关资源
    最近更新 更多