[英]TypeError: 'str' object is not callable (python 2)
program to check if word starts & ends with same letter 程序检查单词是否以相同字母开头和结尾
def match_letter():
count = 0
for word in words:
if len(word) >=2 and word[0] == word[-1]:
count = count + 1
return count
def main():
words = []
words_list = raw_input('Enter Words: ')
words_list = words_list().split()
for word in words_list:
words.append(word)
count = match_letter()
print 'letter matched %d ' %count
if __name__ == '__main__':
main()
this is my python code, giving an error 这是我的python代码,出现错误
Traceback (most recent call last):
File "D:\Programming\Python\Python 2.7\same_letter.py", line 21, in <module>
main()
File "D:\Programming\Python\Python 2.7\same_letter.py", line 13, in main
words_list = words_list().split()
TypeError: 'str' object is not callable
i am very thankful if anyone can help me.. 如果有人可以帮助我,我将非常感激。
This line has an extra parentheses 这行有一个额外的括号
words_list = words_list().split()
It could just be 可能只是
words_list = words_list.split()
In fact, you have a number of extraneous steps, your code block 实际上,您有许多无关的步骤,您的代码块
words = []
words_list = raw_input('Enter Words: ')
words_list = words_list().split()
for word in words_list:
words.append(word)
Could be reduced to: 可以简化为:
words = raw_input('Enter Words: ').split()
And if I understand your question, I would solve this using slicing 如果我理解您的问题,我将使用切片解决
def same_back_and_front(s):
return s[0] == s[-1] # first letter equals last letter
>>> words = ['hello', 'test', 'yay', 'nope']
>>> [word for word in words if same_back_and_front(word)]
['test', 'yay']
Thanx Cyber.. It works for me. Thanx Cyber ..它为我工作。 this code works for me exactly as i want
这段代码完全适合我的需要
def match_letter(words):
count = 0
for word in words:
if len(word) >=2 and word[0] == word[-1]:
count = count + 1
return count
def main():
words = raw_input('Enter Words: ').split()
count = match_letter(words)
print 'letter matched %d ' %count
if __name__ == '__main__':
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.