[英]What is the mistake that i am doing in my pig latin translator
if a word begins with a vowel the word 'yay' is added at the end of the word.如果单词以元音开头,则在单词末尾添加单词“yay”。
if the word begin with a cosonant or consonant cluster, that consonant or consonant cluster is moved to the end the word followed by 'ay'.如果单词以辅音或辅音群开头,则该辅音或辅音群移到单词的末尾,后跟“ay”。
def vowel(value): value=str(value) vowels=['a','e','i','o','u','A','E','I','O','U'] if value[0]in vowels: value=value+'yay' return(value) def consonant(value): value=str(value) vowels=['a','e','i','o','u','A','E','I','O','U'] b=0 for i in range(len(value)): if a[i] in vowels: b=i break c=a[b:len(a)]+a[0:b]+'ay' return(c) spam='My name is AL SWEIGART and i am 4000 years old.' spam1=spam.split() a=['a','e','i','o','u','A','E','I','O','U'] for i in range(len(spam1)): if spam1[i][0] in a: spam1.insert(vowels(spam1[i]),i) else: spam1.insert(consonant(spam1[i]),i) print(spam1)
TypeError Traceback (most recent call last) in 22 spam1.insert(vowels(spam1[i]),i) 23 else: ---> 24 spam1.insert(consonant(spam1[i]),i) 25 print(spam1) TypeError Traceback(最近一次调用最后一次)在 22 spam1.insert(vowels(spam1[i]),i) 23 else: ---> 24 spam1.insert(consonant(spam1[i]),i) 25 print(spam1 )
in consonant(value) 13 b=i 14 break ---> 15 c=a[b:len(a)]+a[0:b]+'ay' 16 return(c) 17 spam='My name is AL SWEIGART and i am 4000 years old.'在辅音(值) 13 b=i 14 break ---> 15 c=a[b:len(a)]+a[0:b]+'ay' 16 return(c) 17 spam='My name is AL SWEIGART 和我 4000 岁。
TypeError: can only concatenate list (not "str") to list类型错误:只能将列表(不是“str”)连接到列表
First thing first it seems that you mixed up value
with a
in the consonant()
function, and since you refer to a[b:len(a)]
when declaring c
, it seems that Python refers to the a
variable which happens to be a list instead of string ( ['a','e','i','o','u','A','E','I','O','U']
)首先,您似乎在
consonant()
函数中将value
与a
混合在一起,并且由于您在声明c
时引用了a[b:len(a)]
,因此 Python 似乎引用了a
变量,它恰好是一个列表而不是字符串( ['a','e','i','o','u','A','E','I','O','U']
)
In this specific line of code,在这行特定的代码中,
c=a[b:len(a)]+a[0:b]+'ay'
c
is declared as a list instead of string containing every item inside list a
ranging from b
to len(a)
. c
被声明为一个列表,而不是包含列表a
中每个项目的字符串,范围从b
到len(a)
。 The result is then concatenated with a[0:b]
which returns another list, but then you concatenate it with 'ay'
which happens to be a string instead of list.然后将结果与返回另一个列表的
a[0:b]
连接,但随后将它与恰好是字符串而不是列表的'ay'
连接。 Hence, the TypeError
you've mentioned in the question.因此,您在问题中提到的
TypeError
。
The solution itself might be trivial, just change that specific line and the preceeding if statement into解决方案本身可能很简单,只需将该特定行和前面的 if 语句更改为
if value[i] in vowels:
b=i
break
c=value[b:len(value)]+value[0:b]+'ay'
Hope this works!希望这有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.