Python Forum
Trying to find first 2 letter word in a list of words
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to find first 2 letter word in a list of words
#1
Hello

I am trying to get this function working but cannot work out what is wrong. Any help will be gratefully received. My input is:

xs = ["this", "is", "a", "dead", "parrot"]

def find_first_2_letter_word(xs):
    for wd in xs:
        if len(wd) == 2:
            return wd
        return " "
    
print("The first 2 letter word is ", find_first_2_letter_word(xs))
My output is:
Output:
Python 3.8.4 (tags/v3.8.4:dfa645a, Jul 13 2020, 16:46:45) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> ================== RESTART: C:\Users\John\Desktop\testing1.py ================== The first 2 letter word is >>>
As can be seen the program prints the statement but fails to show the first 2 letter word.

Thank you
Reply
#2
Hint: what happens on the first iteration of the for loop (i.e. when wd is "this")?
Reply
#3
Hello ndc85430

I am trying to teach myself Python from How to Think Like a Computer Scientist so I must admit to being very much a tyro, please forgive my lack of knowledge. Now answering your hint I think that the first iteration passing over "this" produces a False and so the function should continue on to the second word in the list "is", which is a 2 letter word so the function should return True and stop iterating at that point and move to the print command outputting "The first 2 letter word is is". I still cannot see why the function does not do this.
Reply
#4
Hi,

Quote:first iteration passing over "this" produces a False and so the function should continue on to the second word in the list "is",

This is not happening, hence the code does not work.

The following loop:
for wd in xs:
        if len(wd) == 2:
            return wd
        return " "
will not loop through the whole list simply because the code checks:
if len(wd) == 2:

- the condition is not met since ,,this,, is 4 characters long.

Therefore the code moves on to:
return " "
- this simply exits the for loop returning empty string " " and the code then moves on to:
print("The first 2 letter word is ", find_first_2_letter_word(xs))
You do not want to exit the loop after the first iteration :) Delete/comment out the line that makes the loop exit and the code should work.
Reply
#5
Hint in more direct flavour: first word will return either word itself (if length is 2) or empty string. For-loop never reaches the second word. Remedy: move return of empty string out of for-loop.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Hello IntrOspective

Very helpful and informative thank you for your time and interest. I have learnt something there. I am using How to Think Like a Computer Scientist to teach myself Python and in that book they stated the function, including the 'return " "'. I find that quite misleading.

Thanks again
Reply
#7
No worries,

Since you mentioned that the code comes from the book - i have just had a look at it again - in fact you can get the code working with ,,return " ",, too.

Have a look at the code in your book and see how exactly is ,,return " ", indented..
The way it was indented in your original code it was in fact part of the loop and hence causing trouble.

Therefore, the code in book is not necessarily misleading :)
Reply
#8
Hello IntrOspective

I have only just seen this - thank you for your continued support.

I have looked in the book and it took me some time to spot it but you are correct, thank you.

I am really struggling to get my head around writing programmes with functions and now have had to seek help on my latest attempt in trying to compute a distance.

Never mind onwards it is.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to check if string contains ALL words from the list? zarize 6 483 Jul-22-2020, 07:04 PM
Last Post: zarize
  How to find the first and last of one of several characters in a list of strings? tadsss 2 381 Jun-02-2020, 05:23 PM
Last Post: bowlofred
  Find index value in List Martin2998 3 496 May-12-2020, 02:17 PM
Last Post: deanhystad
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 461 Apr-22-2020, 02:39 PM
Last Post: deanhystad
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 400 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  Python Speech recognition, word by word AceScottie 6 8,973 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  I want to filter out words with one letter in a string (pig latin translator) po0te 1 456 Jan-08-2020, 08:02 AM
Last Post: perfringo
  How to find something in a list using its index rix 1 464 Dec-20-2019, 04:12 PM
Last Post: stullis
  print a word after specific word search evilcode1 8 814 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  How do you replace a word after a match from a list of words in each line of a file? vijju56 1 956 Oct-17-2019, 03:04 PM
Last Post: baquerik

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020