In: Computer Science
Python 3:
I have a file with 53,000 entries or so of movies, the year they were made, the rating, length, and what genre they are. I need to get user input for the year, or the title, or the rating/genre/length. The genre is a 6 digit collection of 0's and 1's. The placement of 1's determines what genre they are (or a combination of genres). The genre,rating, and length are all one test and return entries that match all three criteria
So, I know I need to take this file and make a list. Then I need to get user input and take that input to go through the file and connect the two, then output what they are looking for. I have this so far but I'm starting to hit a wall and I don't even know if what I've done is correct:
def movie_selector(): file = open('movies.txt','r') new_list = file.readline() choice = input("L - Search all movies. T - Search by title. Y - Search by year. S - Search by rating/genre/length." "Q - Exit the selector.") choice.lower() print(choice) if choice == "l": print(new_list) elif choice == "t": t = input("Title is/contains:") print(t) for x in new_list: new_list = file.readline() if x == new_list[0]: print(new_list[0]) elif choice == 'y': select = int(input("Choose a year between 1880 and 2050.")) print(select) for y in new_list: new_list = file.readline() if select == y: print(y) elif 2050<select<1880: print("Invalid selection, please try again.") print(choice) elif choice == 's': genre = input("Please select a genre: Action - (A), Animation - (N), Comedy - (C), Drama - (D), Documentary - (O), " "Romance - (R).") rating = input("Please choose a rating: G, PG, PG-13, R, NC-17, NR") rating.lower() length = input(int("Please choose a maximum length:")) print(genre) print(rating) print(length)
def movie_selector():
file = open('movies.txt','r')
new_list = file.readline() # incorrect.. read all lines using file.readlines()
choice = input("L - Search all movies. T - Search by title. Y - Search by year. S - Search by rating/genre/length."
"Q - Exit the selector.")
choice.lower() # read lower response back as choice = choice.lower()
print(choice)
if choice == "l": print(new_list)
elif choice == "t":
t = input("Title is/contains:")
print(t)
for x in new_list:
# new_list = file.readline() # incorrect, why are you checking it
# with file, you should check in your dictionary
# if x == new_list[0]: print(new_list[0])
if t in x: print(x) # eeven this is not exact, as x is your
# line from the the file, you only are searching for title probably
elif choice == 'y':
select = int(input("Choose a year between 1880 and 2050."))
print(select)
for y in new_list:
# again same thing, read from your own dictionary new_list
# new_list = file.readline()
# you are already doing it in y, so no need of above statement
# y is a whole line from your file, you need to parse y
# to only take the year.
if select == y: print(y)
elif 2050 < select < 1880: print("Invalid selection, please try again.")
print(choice)
elif choice == 's':
genre = input("Please select a genre: Action - (A), Animation - (N), Comedy - (C), Drama - (D), Documentary - (O), "
"Romance - (R).")
rating = input("Please choose a rating: G, PG, PG-13, R, NC-17, NR")
rating.lower() # rating = rating.lower()
length = input(int("Please choose a maximum length:"))
print(genre)
print(rating)
print(length)
This answer may not be very exact for your question, but it will
certainly give you the context in which i am answering to make you
aware about your small coding issues....
You are reading one line at a time from the file, and then going
into your menu selection, taking user input, and again reading one
line from file.. User is doing the operation on all lines of file..
so it is incorrect to read one file, and do operation on that.. the
readline function reads line from file, one at a time.. while
readlines (s at last), will read all the lines
from file together and store it in list, what you want in this
case...
Next time, you post this question for more clarification, plz
include a sample of 10-20 lines only from the give text file and
als, what will be user inputs, and how you expect the responses to
be.. so that we can make a prototype..
please upvote if answer helps you. Thanks!