0

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

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..

2
  • Please add more info! Explain what the code does, what it is that you want it to do, besides the error. Commented Oct 17, 2014 at 22:58
  • this code checks if words in list starts and ends with same letter or not.. Commented Oct 18, 2014 at 7:35

2 Answers 2

5

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']
Sign up to request clarification or add additional context in comments.

Comments

0

Thanx Cyber.. It works for me. 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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.