Feb-15-2021, 01:39 PM
Lots of issues. Here is yours with comments:
lst = ["able", "been", "state", "Sam", "beer"] length = len(lst) # you don't use this def test(lst): #needs a better name, like maybe count_to_sam length = len(lst)# this is the end of the function. It does not return anything for w in lst: #starting the only part of the program that does anything you use if w == "Sam": break print(w)# this loop prints the items up to "Sam" but does not count them. And, by assignment, should be in the function print(test(lst)) #Since test() does not return anything, this prints "None"Here is your project better organized. Look closely, kept to pretty elementary Python
lst = ["able","been","state","Sam","beer"] def count_to_sam(samlist): #better name for function return_count = 0 #this will be the count of words for element in samlist: #loop is in the function if element == "Sam": break return_count = return_count+1 #or use return_count += 1 return return_count #function returns the count print(count_to_sam(lst))