我的一个 python 文件有一个变量 x 的递增数字 如何导入该变量 x 和 运行 它在另一个 python 文件上所以显示 x 递增
One of my python file has increment numbers for a variable x how to import that variable x and run it on another python file so displays x increasing
This is the picture of the output of com1
This is output of com2
我的第一个 python 文件名为“com1”,它有一个代码,每 3 秒增加一次 x 和 y 的值代码是:
x=0
y=0
for i in range(500):
x = x+1
print (x)
y = y+1
print (y)
sleep(3)
我的第二个代码名为“com2”,我使用了以下几行代码:
from com1 import x
from com1 import y
z = x + y
print(z)
这不是打印 z 即 x+y,而是仅打印 x 和 y 的值
谁能告诉我如何修改以便在我的第二个 python 文件 com2 中我可以获得 z 的输出?
检查这个是否满足你的情况
comm1.py
的内容
import time
from comm2 import inc
obj = inc()
for i in range(500):
x,y = obj.increment()
print(x+y)
time.sleep(3)
comm2.py
的内容
class inc:
def __init__(self):
self.x = 0
self.y = 0
def increment(self):
self.x = self.x+1
self.y = self.y+1
return self.x,self.y
This is the picture of the output of com1
This is output of com2
我的第一个 python 文件名为“com1”,它有一个代码,每 3 秒增加一次 x 和 y 的值代码是:
x=0
y=0
for i in range(500):
x = x+1
print (x)
y = y+1
print (y)
sleep(3)
我的第二个代码名为“com2”,我使用了以下几行代码:
from com1 import x
from com1 import y
z = x + y
print(z)
这不是打印 z 即 x+y,而是仅打印 x 和 y 的值 谁能告诉我如何修改以便在我的第二个 python 文件 com2 中我可以获得 z 的输出?
检查这个是否满足你的情况
comm1.py
的内容import time
from comm2 import inc
obj = inc()
for i in range(500):
x,y = obj.increment()
print(x+y)
time.sleep(3)
comm2.py
的内容class inc:
def __init__(self):
self.x = 0
self.y = 0
def increment(self):
self.x = self.x+1
self.y = self.y+1
return self.x,self.y