当我几次尝试停止运行后尝试同时运行这两个脚本时?这是因为导入模块吗?
test1.py
test = input("Go to new script?: ")
if test=="yes":
print("going to new script")
import test2
test2.py
test = input("Go to old script?: ")
if test=="yes":
print("going to new script")
import test1
错误是它本身会结束。
C:\Users\bj\Desktop>python test1.py
Go to new script?: yes
going to new script
Go to old script?: yes
going to new script
Go to new script?: yes
going to new script
C:\Users\bj\Desktop>
答案 0 :(得分:1)
import
记住已经导入的文件,并且不会再次导入它们。
最好将代码放入函数中,并将函数从第二个文件导入到第一个文件,然后在循环中运行它。第二个功能应使用return
返回第一个功能。
test2.py
def func2():
while True:
answer = input("Go to old script?: ")
if answer.lower() == "y":
print("Going back to old script")
return
test1.py
from test2 import func2
def func1():
while True:
answer = input("Go to new script?: ")
if answer.lower() == "y":
print("Going to new script")
func2()
func1()