在两个文件之间共享功能

时间:2019-11-03 14:43:25

标签: python python-3.x

我正在创建一个测验程序,其中有一个供用户输入答案的前端文件和一个后端问题存储文件。我想在两个文件之间共享在每个文件中定义的功能,但是我似乎只能共享一个文件与另一个文件的功能,而不能共享两个文件的功能

我尝试在file1中执行以下操作:从file2导入function2,然后在file2中执行中:从file1导入function1。但是,这不起作用。我刚收到一个无法导入的function1错误。

#file1 (backend)
from physicstester_frontend import answer

def quantum_test():

    while True:
        qq1 = "describe the experiments that lead to wave particle duality."
        print(qq1)
        answer()
        qq1_words = ["double slit", "diffraction", "interference", "wave", "photoelectric", "effect", "photon"]
        if all(word in ans for word in qq1_words):
            print("correct") #prints correct if all required words are in string
            break  
        else:
            print("not a full description, try again")
            continue

#file2 (frontend)
from physicstester_data import quantum_test

print("physics tester")
print("topics: \nQuantum\n..\n..\n..")
topics = input("which topic would you like to be tested on?: ")

if topics.lower() == "quantum":
    quantum_test()

def answer():
    global ans
    ans = input("answer: ")

当我这样做时,我会得到

ImportError: cannot import name 'quantum_test' from 'physicstester_data'

1 个答案:

答案 0 :(得分:0)

import周期不好。

def answer()放入其自己的文件answer.py中,以中断循环。