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
#13
I edited the code a bit to make it a little easier hopefully. Although now im running into a problem with the keep function. It will work if used in the order of the commands but if I want to keep a random movie at say the second step it throws a
   (favourite[-1])
IndexError: list index out of range
My code now looks like this not sure if thats better or worse XD

import random
import sys
from random import choice, sample

print("***Movie Title Explorer***")


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")

def rand ():
           print("Random choice selected")
           print(random.choice(movies_list))

def search():
   list = movies_list
   keyword = str(input("Search keyword: "))
   for movie in movies_list:
       if keyword in movie:
           print(movie)

def startsw():
       letter = str.upper(input("Search title starting with the letter: "))
       for movie in movies_list:
           if movie.startswith(letter):
               favourite.append(movie)
               print(movie)

def keep():
       favourite = []
       movies_list = favourite
       (favourite[-1])
       print("Last movie displayed saved to Favourites")

def showfav():
       print("Your Favourites List")
       print(movies_list[-1])

def clearfav():
       del favourite[:]
       print("Favourites List has been cleared")
def quit():
       print("Bye!")
       sys.exit()




menu()
print("command = ?")

selection=input("Please Select:")
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")
else:
   print ("Unknown Command")

menu()
selection = input("Please Select:")
if selection =='r':
   rand ()
elif selection =='s': search()
elif selection =='k': keep()
elif selection =='f': showfav()
elif selection == 'q': quit()
else:
   print ("Unknown Command")

menu()
selection = input("Please Select:")
if selection =='s':
   search()
elif selection =='r': rand()
elif selection =='s': search()
elif selection =='k': keep()
elif selection =='f': showfav()
elif selection == 'q': quit()
else:
   print ("Unknown Command")
menu()
favourite=[]
selection = input("Please Select:")
if selection =='sw':
   startsw()
elif selection =='s': search()
elif selection =='r': rand()
elif selection =='k': keep()
elif selection =='f': showfav()
elif selection == 'q': quit()
else:
   print("Unknown Command")

menu()
selection = input("Please Select:")
if selection =='k':
   keep()
elif selection =='s': search()
elif selection =='r': rand()
elif selection =='k': keep()
elif selection =='f': showfav()
elif selection == 'q': quit()
else:
   print ("Unknown Command")

menu()

selection = input("Please Select:")
if selection =='f':
   showfav()
elif selection =='s': search()
elif selection =='r': rand()
elif selection =='k': keep()
elif selection =='f': showfav()
elif selection == 'q': quit()
else:
   print ("Unknown Command")

menu()

selection = input("Please Select:")
if selection =='c':
   clearfav()
elif selection =='s': search()
elif selection =='r': rand()
elif selection =='k': keep()
elif selection =='f': showfav()
elif selection == 'q': quit()
else:
   print ("Unknown Command")

menu()
selection = input("Please Select:")
if selection =='q':
   print("Bye!")
   sys.exit()
elif selection =='s': search()
elif selection =='r': rand()
elif selection =='k': keep()
elif selection =='f': showfav()
elif selection == 'q': quit()
else:
   print ("Unknown Command")
Reply


Messages In This Thread
RE: Help printing any items that contains a keyword from a list - by Liquid_Ocelot - May-06-2017, 09:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Collisions for items in a list Idents 3 2,327 Apr-06-2021, 03:48 PM
Last Post: deanhystad
  Removing items in a list cap510 3 2,369 Nov-01-2020, 09:53 PM
Last Post: cap510
  Help with Recursive solution,list items gianniskampanakis 8 3,646 Feb-28-2020, 03:36 PM
Last Post: gianniskampanakis
  Removing items from list slackerman73 8 4,483 Dec-13-2019, 05:39 PM
Last Post: Clunk_Head
  Find 'greater than' items in list johneven 2 4,497 Apr-05-2019, 07:22 AM
Last Post: perfringo
  How to add items within a list Mrocks22 2 2,715 Nov-01-2018, 08:46 PM
Last Post: Mrocks22
  printing list of random generated rectangles Zatoichi 8 7,402 Feb-18-2018, 06:34 PM
Last Post: buran
  How to keep duplicates and remove all other items in list? student8 1 4,977 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