我正在写一个用西班牙语结合动词的基本程序。我目前有两个文件:main.py和test.py.我正在使用test.py来测试该函数。 目前main.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)
最后我试着调用test.py上的testt函数test.py有:
import main
def testt(ending):
if ending == "ar":
form = raw_input("Form: ")
if form == "yo":
return form + " " + stem + "o"
我的错误是:
Traceback (most recent call last):
File "/home/ubuntu/workspace/main.py", line 1, in <module>
import test
File "/home/ubuntu/workspace/test.py", line 1, in <module>
import main
File "/home/ubuntu/workspace/main.py", line 19, in <module>
test.testt(end)
AttributeError: 'module' object has no attribute 'testt'
我正在使用python 2。
将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"
此外,
stem
不会在test.py
中工作,因为它在main.py
中定义
您正在导入test
作为present
。而不是使用test.testt()
使用present.testt()
。此外,您的代码遭受circular import
问题。 Circular Import Problem