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

Python 3 Fix the code so the program reads the file and see if the bar...
Python 3 Fix the code so the program reads the file and see if the bar code was already inputted 3 times if so, it ishows a warning indicating that the item was already tested 3 times Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox from datetime import datetime window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # 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,...
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...
I have an excel file imported into canopy (python) using import pandas as pd. The excel...
I have an excel file imported into canopy (python) using import pandas as pd. The excel file has headers titled: datetime created_at PM25 temperatureF dewpointF    humidityPCNT windMPH    wind_speedMPH wind_gustsMPH pressureIN precipIN these column headers all have thousands of data numbers under them. How could i find the average of all of the numbers in each column and plot them on 1 graph (line graph or scatter plot) Thank you.(please comment out your code)
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)...
Python 3 Fix the code so everytime i hit clear the calendar resets to the current...
Python 3 Fix the code so everytime i hit clear the calendar resets to the current date in case it was modified Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # 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...
Python 3 Fix the code so everytime i hit clear the calendar resets to the current...
Python 3 Fix the code so everytime i hit clear the calendar resets to the current date in case it was modified Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # 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...
Python 3 Fix the code so everytime i hit clear the calendar resets to the current...
Python 3 Fix the code so everytime i hit clear the calendar resets to the current date in case it was modified Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT