[英]calling modules in a python file more than once
我正在嘗試僅使用python制作一個非常基本的操作系統,它的內容如下:
print ("welcome to ben's operating system V 0.1.0")
ten = 0
while (ten < 1000000):
do = input()
if do == ("pythag"):
from bensos import pythag
elif do == ("word"):
from bensos import word
else :
print ("invalid input")
pythag代碼是這個
from math import sqrt
a = float(input ("a="))
b = float(input ("b="))
a = a*a
b = b*b
c = a+b
c = sqrt (c)
print ("c=")
print (c)
d = input("end")
我只有兩個程序,“一個”一詞不起作用,我還沒有完善的循環程序,但它可以工作。 我遇到的問題是重新加載代碼。 我將其放在底部,並指出某種錯誤,因此我嘗試將其放入模塊中,但仍然出現錯誤。 沒有重新加載,它運行良好,但是我只能使用一次模塊,而且到處都看不到任何幫助。
您最好將所有進口商品放在頂部,所以
from bensos import pythag
from bensos import word
...
然后在pythag和word模塊中定義一個過程runModule,並用模塊中當前存在的所有代碼填充該過程。
def runModule():
#The contents of your files at the moment
所以你的pythag文件看起來像
from math import sqrt
def runModule():
a = float(input ("a="))
b = float(input ("b="))
a = a*a
b = b*b
c = a+b
c = sqrt (c)
print ("c=")
print (c)
d = input("end")
這樣,您的代碼將首先加載模塊,然后您可以在需要時隨時調用它們
pythag.runModule()
讓我知道是否不清楚,或者您仍然遇到麻煩。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.