英文:
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论