Python Script Referencing or Calling

huangapple go评论65阅读模式
英文:

Python Script Referencing or Calling

问题

I have 2 python files (.py) named fileA and fileB.

file A has the following codes:

import Pandas as pd
x = input (x)
z = input (y)
y = x + z

if y == 8:
fileB()

Call or run file B

::::::::::::::::::::::::::::::::::::::::::::::::::::

file B has the following codes:

x and y are from file A

a = x + y
c = a + z
print (c)

Question: I want to import file B into A and run file A

I have tried to use the following:

into file A

from fileB import*

and also from fileA import*

into file B so I can use some variables in file A.

But I get errors as name x and y are not defined for fileB

英文:

I have 2 python files (.py) named fileA and fileB.

# file A has the following codes:

import Pandas as pd
x = input (x)
z = input (y)
y = x + z

if y == 8:
    fileB() 
# Call or run file B
# ::::::::::::::::::::::::::::::::::::::::::::::::::::
# file B has the following codes:
# x and y are from file A
a = x + y     
c = a + z
print (c)

Question: I want to import file B into A and run file A

# I have tried to use the following:

# into file A
from  fileB import* 

# and also from fileA import* #
# into file B so I can use some variables in file A.

# But I get errors as name x and y are not defined for fileB

答案1

得分: 0

如果您在文件B中创建一个执行数学计算的函数,它应该能够工作:

def funB(x, z):
    a = x + y     
    c = a + z
    return c

然后您可以在文件A中像这样导入该函数:

from fileB import funB
if ...:
    print(funB(x, y))
英文:

If you create a function in file B that does the maths it schould work:

def funB(x, z):
    a = x + y     
    c = a + z
    return c

then you can import that funcion in file A like:

from fileB import funB
if ...:
    print(funB(x, y))

huangapple
  • 本文由 发表于 2023年5月17日 16:26:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定