猪拉丁语翻译工作不正常。没有显示元音'a'的输出。
示例:输入:va
输出是显示参考错误的错误。 除了'a'之外,所有其他元音都能正常工作。
UnboundLocalError:赋值前引用的局部变量'list3'。
import string
def translate(str1):
str2="eaiouy"
list2=[]
punstr=""
for c in str1:
if c in string.punctuation:
punstr=punstr+c
for c in string.punctuation:
str1= str1.replace(c,"")
if str1.isdigit():
return str1+punstr
else:
if (len(str1)==1):
if str1[0] in str2:
return str1+"yay"+punstr
else:
return str1+"ay"+punstr
elif str1[0] in str2:
return str1+"yay"+punstr
else:
for i in str2:
list1=str1.split(i)
if (len(list1[0])<len(list2)):
list3=list1[0]
list2=list1[0]
prestr=str(list3)
stem=str1.split(list3)
reqstem=stem[1]
return reqstem+prestr+"ay"+punstr
while True:
str1=raw_input("\nenter the sentance")
sentlist=str1.split(" ")
for i in range(len(sentlist)):
mystr=sentlist[i]
if i==0:
reqstring=translate(mystr)
print reqstring.capitalize(),
else:
reqstring=translate(mystr)
print reqstring,
答案 0 :(得分:1)
list3
仅分配到len(list1[0]<len(list2)
时,这意味着有时未设置,而prestr=str(list3)
会给您带来麻烦。
答案 1 :(得分:0)
现在工作!!!
import string
def translate(str1):
vowel=" eaiouy"
list2=[]
punstr=""
for c in str1:
if c in string.punctuation:
punstr=punstr+c
for c in string.punctuation:
str1= str1.replace(c,"")
if str1.isdigit():
return str1+punstr
else:
if (len(str1)==1):
if str1[0] in vowel:
return str1+"yay"+punstr
else:
return str1+"ay"+punstr
elif str1[0] in vowel:
return str1+"yay"+punstr
else:
for i in str1:
if i in vowel:
list1=str1.split(i)
print list1
break
prestr=str(list1[0])
stem=str1.split(prestr)
reqstem=stem[1]
return reqstem+prestr+"ay"+punstr
while True:
str1=raw_input("\nenter the sentance")
sentlist=str1.split(" ")
for i in range(len(sentlist)):
mystr=sentlist[i]
if i==0:
reqstring=translate(mystr)
print reqstring.capitalize(),
else:
reqstring=translate(mystr)
print reqstring,