May-06-2017, 09:00 PM
(This post was last modified: May-06-2017, 09:12 PM by Liquid_Ocelot.)
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 rangeMy 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")