# Allison Obourn
# CSC 110, Spring 2018
# Lecture 21

# This program searches a part of the IMDB movie database
# for a word or phrase a user inputs. It expects the input file
# to contain information about one movie per line and be in the format
# <rank> <rating> <votes> <name>

def main():
    word = search_word()
    file = open("imdb.txt")
    lines = file.readlines()
    count = 0
    for i in range(len(lines)):
        if word in lines[i].lower():
            # only output the table titles if this is the first
            # movie that we have found
            if count == 0:
                print("Rank\tVotes\tRating\tTitle")
            output(lines[i])
            count += 1
    if count == 1:
        print("1 match.")
    else:
        print(count, "matches.")

# prompts the user for a word or phrase to search for
def search_word():
    word = input("Search word? ")
    word = word.lower()
    return word


# outputs the passed in line in the order of rank, votes, rating, title
# with each separated by a tab.
def output(line):
    text = line.split()
    print(text[0] + "\t" + text[2] + "\t" + text[1] + "\t", end='')
    for i in range(3, len(text)):
        print(text[i] + " ", end='')
    print()

main()
