0

I'm writing a code that takes movie names and release dates from a txt file and turns it into a dictionary. Then there is two other codes. First code asks the user to type a year and adds the movies released in that year into a list. Second code asks the user for a letter and adds movies starting with that letter(and their release dates) into a list. First code works fine but the second code ignores the if statement and adds all of the movies to the list. This was supposed to happen when I typed "u"

[1996 Unhook the Stars, 2005,Up and Down,1996,Unhook the Stars, 1987,Unsolved Mysteries: Psychics, 2004,Unconstitutional: The War on Our Civil Liberties]

My code is :

movies = {}
years = []
names = []
def load_movies():
    with open("movie_data.txt", "r") as document:
        for line in document:
            (k, v) = line.split(",")
            movies.update({k : (movies[k] if k in movies else []) + [v]})
def get_movies_by_year():
    x = str(input("Enter a year: "))
    for i , t in movies.items():
        if x == i :
            years.insert(1, (t))
    #print_list(years)
def get_movies_by_name():
    o = input("Enter a letter: ")
    for y , q in movies.items():
        for value in q:
            if (value[0]) == o.upper() or o.lower() :
                names.insert(1, (y , value))
    print_list(names)
         
def print_list(e):
    for l in e:
    for w in l:
        print(w.strip("\n"))


load_movies()
get_movies_by_name()
petezurich
  • 9,280
  • 9
  • 43
  • 57
benegos
  • 9
  • 2
  • 1
    use this one: `if value[0].lower() == o.lower() :` OR `if (value[0].upper()) == o.upper() :` – Tula Magar Jul 06 '22 at 19:13
  • `if (value[0]) == o.upper() or o.lower() :` is incorrect. You meant to do: `if (value[0]) == o.upper() or value[0] == o.lower() :` – crock Jul 06 '22 at 19:14
  • @crock is there anything I can do to print the year and the name of the movie side by side. It worked but it prints the release date on a line and movie name on another line. – benegos Jul 06 '22 at 19:20
  • Welcome to Stack Overflow! Please take the [tour] and read [ask], which has tips like how to write a good title. For debugging help in the future, it's important to provide a [mre], meaning some example input (movie data), clear expected output (since this output looks like a Python list, but that doesn't seem to be what it is, and the commas are inconsistent), and only enough code to reproduce the issue (for example, `get_movies_by_year` is irrelevant so can be removed). – wjandrea Jul 06 '22 at 19:25
  • multiple prints on same line: https://stackoverflow.com/questions/5598181/multiple-prints-on-the-same-line-in-python – crock Jul 06 '22 at 20:02

0 Answers0