[英]Why can't word.lstrip be assigned to function call?
def pig_latin(text):
say = ""
# Separate the text into words
words = text.split()
for word in words:
# Create the pig latin word and add it to the list
word.lstrip(-1) += (word[0]+"ay")
say.append(word)
# Turn the list back into a phrase
return say
print(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun")) # Should be "rogrammingpay niay ythonpay siay unfay"
在 word.lstrip 上出现语法错误。我可以在这里找到答案我只需要知道我对 .lstrip 甚至对于循环不了解的内容。
" hi ".lstrip(-1)
在我的机器上返回此错误: TypeError: lstrip arg must be None or str
。
" hi ".lstrip()
返回'hi '
。
"tttthi ".lstrip("t")
返回'hi '
。
lstrip()
方法只接受字符串参数或根本不接受参数。 您提供的是 integer 参数,这就是它出错的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.