Question

In: Computer Science

Python 3: I have a file with 53,000 entries or so of movies, the year they...

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)
        

Solutions

Expert Solution

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!


Related Solutions

This is all in Python. Also, the names come from a .txt file that i have...
This is all in Python. Also, the names come from a .txt file that i have created with 3 names on it(Jim,Pam,Dwight). Main Function. Write a program where the user enters a name. Using the read function, check to see if the name is in the text document. If it is, respond back to the user the name is found within the list. Read Function. The read function should return the contents of the text document as a list. EXAMPLE...
Python 3 Fix the code so i can make the window larger or smaller and the...
Python 3 Fix the code so i can make the window larger or smaller and the fields adjusts everytime according to the window size import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook window = tk.Tk() window.title("daily logs") # window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=4, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window)...
I have a Python code that reads the text file, creates word list then calculates word...
I have a Python code that reads the text file, creates word list then calculates word frequency of each word. Please see below: #Open file f = open('example.txt', 'r') #list created with all words data=f.read().lower() list1=data.split() #empty dictionary d={} # Adding all elements of the list to a dictionary and assigning it's value as zero for i in set(list1):     d[i]=0 # checking and counting the values for i in list1:     for j in d.keys():        if i==j:           d[i]=d[i]+1 #Return all non-overlapping...
Python 3 Fix the code so i can make the window larger and the fields increase...
Python 3 Fix the code so i can make the window larger and the fields increase size import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook window = tk.Tk() window.title("daily logs") # window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=4, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money = tk.Entry(window) #...
Python 3 Fix the code so i can make the window larger and the fields increase...
Python 3 Fix the code so i can make the window larger and the fields increase size Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook window = tk.Tk() window.title("daily logs") # window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=4, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money = tk.Entry(window)...
Please Use Python 3.The Problem: Read the name of a file and then open it for...
Please Use Python 3.The Problem: Read the name of a file and then open it for reading. Read the name of another file and then open it for output. When the program completes, the output file must contain the lines in the input file, but in the reverse order. • Write the input, output, and the relationship between the input and output. • Develop the test data (A single test will be alright). Make the input file at least 3...
I am trying to make a program in Python that opens a file and tells you...
I am trying to make a program in Python that opens a file and tells you how many lines, vowels, consonants, and digits there are in the file. I got the print out of lines, digits, and consonants, but the if statement I made with the vowels list isn't recognizing them. How can I make the vowels go through the if statement with the list? Am I using the wrong operator? Here is my program #Assignment Ch7-1 #This program takes...
Can you please solve these questions/ statements using python? I started with "importing" the file. I...
Can you please solve these questions/ statements using python? I started with "importing" the file. I only need question one to be answered not two-four. Can use whatever data frame of choice, I just need a sample code for each line. Thank you #1. #Fit a linear regression model on data: USA_housing.csv to predict the Price of the house. import pandas as pd housing_df = pd.read_csv("USA_Housing.csv") #Income: Avg. area income #Age: Avg age of the houses #Bedrooms: Avg No of...
How would I create a nested dictionary given a csv file in Python? Say I want...
How would I create a nested dictionary given a csv file in Python? Say I want to make a dictionary that read {'country':{'China':'Fit', 'China':'Overweight', 'USA': 'Overweight', 'USA': 'Fit', 'England':'Fit'...}, 'category':{'Asian':'Fit', 'Caucasian': 'Overweight', 'Caucasian':'Overweight', 'Asian': 'Fit', 'Middle Eastern': 'Fit'...}} given a file that had country category Weight China Asian Fit China Caucasian Overweight USA Caucasian Overweight USA Asian Fit England Middle Eastern Fit... ... And so on in the file.
Send this as a Python file. Note: this is an example where you have the original...
Send this as a Python file. Note: this is an example where you have the original file, and are writing to a temp file with the new information. Then, you remove the original file and rename the temp file to the original file name. Don't forget the import os statement.A file exist on the disk named students.txt The file contains several records and each record contains 2 fields :1. The student's name and 2: the student's score for final exam....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT