Feb-15-2021, 09:20 AM
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:
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
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