Python Forum
Function to count words in a list up to and including Sam
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function to count words in a list up to and including Sam
#3
(Feb-15-2021, 09:20 AM)Oldman45 Wrote: Good Morning
I am running Python 3.8.5 and trying to develop a function that counts the number of words in a list up to and including the word Sam. Here is what I have developed so far:
lst = ["able", "been", "state", "Sam", "beer"]
length = len(lst)
def test(lst):
    length = len(lst)
for w in lst:
    if w == "Sam":
        break
    print(w)
print(test(lst))
The out put I get is:
able
been
state
None

I would appreciate input to:
1. Help me understand why I cannot get the answer as a number (4)
2. Understand why I have to state 'length = len(lst) as a variable outside of the function, then repeat it as the first line of the function

Thanks in advance

1. You don't get the answer as a number because that's not what you ask for.
2. You set the variable length in two places but you never use it.

Did you put this code together yourself? It seems odd when someone presents code that they don't understand.

In the first line you declare the list "lst". No problem...

In the next line you set length (but you never use it)

Then there are the two lines (3 and 4) where you delcare the function "test":
def test(lst):
    length = len(lst)
The function calculates the length of the list but never returns any value, meaning that the function, when called, will return the default value "None".

The next lines (5 - 8), are not part of the function as they are not indented to be part of it. So, these lines are called first, giving the output:
Output:
able been state
Then you call the function "test" and print its return value, which, as mentioned above, is "None".

You need to study some basic python tutorial. This is too messy to be corrected. A function to give you the number of elements in a list up to a certain element can be done in many ways. The most obvious is to create a counter and then scan through the list, adding 1 to the counter for each element in the list and break after encountering the searched-for element, returning the value of the counter as the result unless you went trough the whole list without finding the element in question. Then you return 0 or whatever is stipulated as a "did not find"-value.

Another approach is to find the index for the element in question and return that index + 1 but that requires catching an error if the element is not in the list so you would need a little more than the most basic knowledge of python. You don't need the length of the list unless you decide to use it as a stop value when going through the list.
Reply


Messages In This Thread
RE: Function to count words in a list up to and including Sam - by Serafim - Feb-15-2021, 12:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,438 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,761 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  How to get unique entries in a list and the count of occurrence james2009 5 3,078 May-08-2022, 04:34 AM
Last Post: ndc85430
  Including data files in a package ChrisOfBristol 4 2,680 Oct-27-2021, 04:14 PM
Last Post: ChrisOfBristol
  Not including a constructor __init__ in the class definition... bytecrunch 3 12,332 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  count item in list korenron 8 3,590 Aug-18-2021, 06:40 AM
Last Post: naughtyCat
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,907 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  list.count does not appear to function Oldman45 7 4,080 Mar-16-2021, 04:25 PM
Last Post: Oldman45
  how to create pythonic codes including for loop and if statement? aupres 1 1,977 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  How to use the count function from an Excel file using Python? jpy 2 4,544 Dec-21-2020, 12:30 AM
Last Post: jpy

Forum Jump:

User Panel Messages

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