在两个文件之间共享函数

2024-09-29 21:33:10 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我试过在file1中执行:从file2导入函数2,然后在file2中执行:从file1导入函数1。但是这不起作用。我刚刚得到一个无法导入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'

Tags: 文件函数answerinfromtestimportif

热门问题