从python中的其他模块调用函数

时间:2019-02-27 04:00:59

标签: python function import module

我正在写一个基本程序来用西班牙语修饰动词。我目前有两个文件:main.py和test.py。我正在使用test.py来测试功能。
当前main.py具有:

"before read"

最后,我尝试调用test.py上的testt函数 test.py具有:

import test as present

print("Welcome to Spanish Verb Conjugator")
verb = raw_input("Enter the verb: ")
length = len(verb)

#print(length)

v1 = length - 2
r1 = length - 1
v = verb[v1]
r = verb[r1]
end = str(v+r)
stem = verb[0:v1]


tense = raw_input("Choose your tense: ")
if tense == "present":
    test.testt(end)

我的错误是:

import main 

def testt(ending):
    if ending == "ar":
        form = raw_input("Form: ")
        if form == "yo":
            return form + " " + stem + "o"

我正在使用python 2。

2 个答案:

答案 0 :(得分:1)

将main.py中的代码更改为:

import test 

print("Welcome to Spanish Verb Conjugator")
verb = raw_input("Enter the verb: ")
length = len(verb)

#print(length)

v1 = length - 2
r1 = length - 1
v = verb[v1]
r = verb[r1]
end = str(v+r)
print end
stem = verb[0:v1]


tense = raw_input("Choose your tense: ")
if tense == "present":
    test.testt(end)

并将test.py更改为:

def testt(ending):
    if ending == "ar":
        form = raw_input("Form: ")
        if form == "yo":
            return form + " " + stem + "o"

此外

stemtest.py中定义的main.py中将不起作用

答案 1 :(得分:0)

您正在将test导入为present。不要使用test.testt(),而要使用present.testt()。此外,您的代码还存在circular import问题。 Circular Import Problem