這是com1輸出的圖片
這是 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)
這不是列印 x y 的 z,而是只列印 x 和 y 的值誰能告訴我如何修改以便在我的第二個 python 檔案 com2 中我可以獲得 z 的輸出?
uj5u.com熱心網友回復:
嗨,檢查這是否滿足您的情況
comm1.py 的內容
import time
from comm2 import inc
obj = inc()
for i in range(500):
x,y = obj.increament()
print(x y)
time.sleep(3)
comm2.py 的內容
class inc:
def __init__(self):
self.x = 0
self.y = 0
def increament(self):
self.x = self.x 1
self.y = self.y 1
return self.x,self.y
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/329925.html
