Python Forum
Help printing any items that contains a keyword from a list
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help printing any items that contains a keyword from a list
#14
You are repeating the same thing over and over again. Have the program do that for you. That's what loops are for.

while True:
    choice = input('Pick a letter: ').lower()
    if choice == 'a':
        print('The first letter')
    elif choice == 'z':
        print('The last letter')
    elif choice == 'q':
        break
    else:
        print('Some other letter')
Notice how I use lower. It's better to get it from the string you are lowering (the result of input) than to get it from the str class.

The problem with favorite is a scope problem. The favorite you define in a function only exists in that function. That's its scope. It is not the same as any favorite you define outside the function. What you want to do is return the value from the function:

def search(movie_list):
    keyword = input('Search keyword: ').lower()
    matches = []
    for movie in movies_list:
        if keyword in movie.lower():
            print(movie)
            matches.append(movie)
    return matches
First of all, notice how I pass movie_list as a parameter to the function. This is again about scope. That puts movie_list into the scope of the function. Note that I don't name things list or file. We have told you repeatedly that is a bad idea, and you haven't changed it. Those are keywords in Python, and if you change them it could mess up some other part of Python you are trying to make use of. Then I build a list of matches (as I showed you before), and return it from the function. When the function is called, it would look like this:

matches = search(movie_list)
Then you have the matches in the scope of the while loop. You can then add items from it to the favorites list that you defined in that same scope.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: Help printing any items that contains a keyword from a list - by ichabod801 - May-06-2017, 10:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Collisions for items in a list Idents 3 2,300 Apr-06-2021, 03:48 PM
Last Post: deanhystad
  Removing items in a list cap510 3 2,347 Nov-01-2020, 09:53 PM
Last Post: cap510
  Help with Recursive solution,list items gianniskampanakis 8 3,594 Feb-28-2020, 03:36 PM
Last Post: gianniskampanakis
  Removing items from list slackerman73 8 4,446 Dec-13-2019, 05:39 PM
Last Post: Clunk_Head
  Find 'greater than' items in list johneven 2 4,473 Apr-05-2019, 07:22 AM
Last Post: perfringo
  How to add items within a list Mrocks22 2 2,682 Nov-01-2018, 08:46 PM
Last Post: Mrocks22
  printing list of random generated rectangles Zatoichi 8 7,358 Feb-18-2018, 06:34 PM
Last Post: buran
  How to keep duplicates and remove all other items in list? student8 1 4,947 Oct-28-2017, 05:52 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

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