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
#12
Basically, yes. though your indentation is off.  All of your 'if' become 'elif' with the 'else' statement being the failsafe should all else fail.

While we're at it, this = not good

def menu(): print("l – load file of movie titles"'\n'"r – random movie"'\n'"s– search"'\n'""
      "sw – starts with"'\n'"k – keep - save the last displayed movie title to your favourites"'\n'"f – favourites display"'\n'""
      "c – clear"'\n'"q - quit program")
this = better

def menu():
    print("l – load file of movie titles\nr – random movie\ns– search\nsw – starts with\n"
          "k – keep - save the last displayed movie title to your favourites\nf – favourites display\nc – clear\n"
          "q - quit program")
This = bad

l = file = open("C:\\Users\\LiquidO\\Desktop\\Assingment 2 python\\movies.txt", "r")
if selection =='l':
    movies_list = file.readlines()
    movies_list = [movie.strip() for movie in movies_list]
    print("Movies now loaded")
Why assign to a variable 'l' plus you must manually close the file

This = better

if selection == 'l':
    with open("C:/Users/LiquidO/Desktop/Assignment 2 python/movies.txt", "r") as file:
        movies_list = file.readlines()
        movies_list = [movie.strip() for movie in movies_list]
        print("Movies now loaded")
File will be closed automatically.

Why assign a print function to 'r', and not use 'r'? Just print it.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Messages In This Thread
RE: Help printing any items that contains a keyword from a list - by sparkz_alot - May-06-2017, 08:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 1 607 May-23-2024, 05:39 AM
Last Post: Pedroski55
  Collisions for items in a list Idents 3 2,455 Apr-06-2021, 03:48 PM
Last Post: deanhystad
  Removing items in a list cap510 3 2,477 Nov-01-2020, 09:53 PM
Last Post: cap510
  Help with Recursive solution,list items gianniskampanakis 8 3,853 Feb-28-2020, 03:36 PM
Last Post: gianniskampanakis
  Removing items from list slackerman73 8 4,663 Dec-13-2019, 05:39 PM
Last Post: Clunk_Head
  Find 'greater than' items in list johneven 2 4,638 Apr-05-2019, 07:22 AM
Last Post: perfringo
  How to add items within a list Mrocks22 2 2,803 Nov-01-2018, 08:46 PM
Last Post: Mrocks22
  printing list of random generated rectangles Zatoichi 8 7,545 Feb-18-2018, 06:34 PM
Last Post: buran
  How to keep duplicates and remove all other items in list? student8 1 5,085 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