Python Script Referencing or Calling

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

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.

  1. # file A has the following codes:
  2. import Pandas as pd
  3. x = input (x)
  4. z = input (y)
  5. y = x + z
  6. if y == 8:
  7. fileB()
  8. # Call or run file B
  9. # ::::::::::::::::::::::::::::::::::::::::::::::::::::
  10. # file B has the following codes:
  11. # x and y are from file A
  12. a = x + y
  13. c = a + z
  14. print (c)

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

  1. # I have tried to use the following:
  2. # into file A
  3. from fileB import*
  4. # and also from fileA import* #
  5. # into file B so I can use some variables in file A.
  6. # But I get errors as name x and y are not defined for fileB

答案1

得分: 0

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

  1. def funB(x, z):
  2. a = x + y
  3. c = a + z
  4. return c

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

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

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

  1. def funB(x, z):
  2. a = x + y
  3. c = a + z
  4. return c

then you can import that funcion in file A like:

  1. from fileB import funB
  2. if ...:
  3. 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:

确定