Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python dictionary
#1
I'm kinda stuck on this homework assignment and i need help. this program has a menu but i got all the menu except this part. this menu selection is asking the user input for the category and going thought the list then print out the movie year and name that contain the category.example of dictionary/ [year,"name", "category"] and the dictionary name is movieDictionary:I need help on the red text

movies = [[1939, "Gone With the Wind", "drama"],
          [1943, "Casablanca", "drama"],
          [1965, "The Sound of Music", "musical"],
          [1969, "Midnight Cowboy", "drama"],
          [1972, "The Godfather", "drama"],
          [1973, "The Sting", "comedy"],
          [1977, "Annie Hall", "comedy"],
          [1982, "Gandhi", "historical"],
          [1986, "Platoon", "action"],
          [1990, "Dances with Wolves", "western"],
          [1992, "Unforgiven", "western"],
          [1994, "Forrest Gump", "comedy"],
          [1995, "Braveheart", "historical"],
          [1997, "Titanic", "historical"],
          [1998, "Shakespeare in Love", "comedy"],
          [2000, "Gladiator", "action"],
          [2001, "A Beautiful Mind", "historical"],
          [2002, "Chicago", "musical"],
          [2009, "The Hurt Locker", "action"],
          [2010, "The Kings Speech", "historical"],
          [2011, "The Artist", "comedy"],
          [2012, "Argo", "historical"],
          [2013, "12 Years a Slave", "drama"],
          [2014, "Birdman", "comedy"],
          [2016, "Moonlight", "drama"],
          [2017, "The Shape of Water", "fantasy"],
          [2020,"Parasite","drama"]]

movieDictionary ={}
for movie in movies:
    year = movie[0]
    movieDictionary[year] = [movie[1],movie[2]]

    elif option == "pc":
        category = input("Enter category: ")
        if category in "action, drama, musical, comedy, historical, western, fantasy ":
            for movie in movieDictionary:
                output4 = []
                output4 = movieDictionary
                if output4[1] == category:
                    print(movie,",",output[0],",",output[1])
Reply
#2
I am not sure what problem you think you are having with input. It works fine for me. I made a couple of changes to your code so I could run it and I could only find dramas. Then I realized I was searching the dictionary while I was building it. Without seeing all your code I cannot say if this is one of your problems or not.

You also need to give this code a good hard look:
            for movie in movieDictionary:
                output4 = []  # <- I'm a list
                output4 = movieDictionary  # <- Now I'm a dictionary
                if output4[1] == category: # <- Did they make movies in 1 A.D.?
                    print(movie,",",output[0],",",output[1])  # <- output4???
Reply
#3
im getting errors key 1 with the if state

movies = [[1939, "Gone With the Wind", "drama"],
          [1943, "Casablanca", "drama"],
          [1965, "The Sound of Music", "musical"],
          [1969, "Midnight Cowboy", "drama"],
          [1972, "The Godfather", "drama"],
          [1973, "The Sting", "comedy"],
          [1977, "Annie Hall", "comedy"],
          [1982, "Gandhi", "historical"],
          [1986, "Platoon", "action"],
          [1990, "Dances with Wolves", "western"],
          [1992, "Unforgiven", "western"],
          [1994, "Forrest Gump", "comedy"],
          [1995, "Braveheart", "historical"],
          [1997, "Titanic", "historical"],
          [1998, "Shakespeare in Love", "comedy"],
          [2000, "Gladiator", "action"],
          [2001, "A Beautiful Mind", "historical"],
          [2002, "Chicago", "musical"],
          [2009, "The Hurt Locker", "action"],
          [2010, "The Kings Speech", "historical"],
          [2011, "The Artist", "comedy"],
          [2012, "Argo", "historical"],
          [2013, "12 Years a Slave", "drama"],
          [2014, "Birdman", "comedy"],
          [2016, "Moonlight", "drama"],
          [2017, "The Shape of Water", "fantasy"],
          [2020,"Parasite","drama"]]

movieDictionary ={}
for movie in movies:
    year = movie[0]
    movieDictionary[year] = [movie[1],movie[2]]
    
#update Function
def update(year):
    movie = [year]
    name = input("Enter movie name:")
    if len(name) > 40:
        print("movie title cannot be greater than 40 characters. Try again")
        return
    movie.append(name)
    category = input("Enter Category:")
    if category not in ['drama', 'western', 'historical', 'musical', 'comedy', 'action', 'fantasy', 'scifi']:
        print("Wrong category. Try again.")
        return
    movie.append(category)
    movies.append(movie)



menu = """
1 - display winning movie by year
2 - display movie and category by year
3 - add movie and category to list for an entered year
p - print entire movie list – year, title, category
pc – print movies in a selected category – year and title
q - quit
Select one of the menu options above
"""
cont = "y"
while cont == "y":
    option = input(menu)

#p
    if option == "p":
        for movie in movieDictionary:
            output = []
            output = movieDictionary[movie]
            print(movie,",",output[0],",",output[1])
        cont = input("Search more movies(y/n)? ")
#1
    elif option == "1":
        update1 = "n"
        continue1 = 0
        output2=[]
        year=int(input("Enter the year :"))
        if year in movieDictionary:
            output2 = movieDictionary[year]
            print("Winning name is :",output2[0])
            continue1 = 1
        else:
            if continue1 == 0:
                update1=input("Movies is not found, Would you like to update? y/n ")
                if update1== "y":
                    update(year)
                    
        cont = input("Search more movies(y/n)? ")
#2
    elif option == "2":
        update2 = "n"
        continue2 = 0
        year = int(input("Enter year to Search: "))
        if year in range(1927, 2021):
            if year in movieDictionary:
                output3=[]
                output3 = movieDictionary[year]
                print("The Oscar winning movie for "+str(year)+" was "+output3[0]+" "+output3[1])
                continue2 = 1
            if continue2 == 0:
                update2=input("Movies is not found, Would you like to update? y/n ")
                if update2== "y":
                    update(year)
        else:
            print("Invalid years")
        cont = input("Search more movies(y/n)? ")
#3
    elif option == "3":
        year=int(input("Enter the year : "))
        movieName=input("Enter the movie name : ")
        category=input("Enter the movie category : ")
        if year in movieDictionary:
            print("Sorry this year's winning movie! try again ")
        else:
            movieDictionary[year]=[movieName,category]
            print("Movies is added")
        cont = input("Search more movies(y/n)? ")
        
    
#pc
    elif option == "pc":
        category = input("Enter category: ")
        if category in "action, drama, musical, comedy, historical, western, fantasy ":
            for movie in movieDictionary:
                output4 = []
                output4 = movieDictionary
                if output4[1] == category:
                    print(movie,",",output[0],",",output[1])
        else:
            print("There isn't any movie that match")
        cont = input("Search more movies(y/n)? ")
#q
    elif option == "q":
        cont = "n"
#invalid options
    else:
        print("invalid menu options, try again.")
        
input ("\n\nHit Enter to end program")

this is the full code
Reply
#4
Hmmmm, works fine for me. I ran the program and entered "1". It asked me to enter a year. I entered 1939 and it prints "Gone With the Wind". I search for more movies by year, enter "2010" and get "The King's Speech". Both correct. Printing all the movies worked correctly. Display movie and category by year worked. Quit kind of worked. Why do I have to print enter again?

I do get an error when I try the "pc" option.
Error:
Traceback (most recent call last): File "C:\Users\djhys\Documents\Python\Musings\junk.py", line 126, in <module> if output4[1] == category: KeyError: 1
The error is related to the code I suggested you review. I think you are expecting output4[] to have the data for a movie. But instead you set it equal to the movie dictionary. When you ask for output4[1] it is the same as movieDictionary[1]. The dictionary does not have an entry with key == 1, so you get a key error.

I think you want to do something like this
for year, movie in movieDictionary.items():

# or this

for year in movieDictionary:  # this returns the keys.  I didn't know about this
    movie = movieDictionary[year]
This will pull every dictionary entry one at a time. The year filed is set the dictionary key, and the movie field is set to the dictionary data. First time thru the year should be '1939' and the movie will be ['Gone With the Wind', 'drama']

Try this in your "pc" loop and determine if it gives you the information you need.
#pc
    elif option == "pc":
        category = input("Enter category: ")
        if category in "action, drama, musical, comedy, historical, western, fantasy ":
            for year, movie in movieDictionary.items():
                print(year, movie)
##                output4 = []
##                output4 = movieDictionary
##                if output4[1] == category:
##                    print(movie,",",output[0],",",output[1])
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020