Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python dictionary
#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


Messages In This Thread
python dictionary - by pkmnmewtwo - Mar-31-2020, 08:47 PM
RE: python dictionary - by deanhystad - Mar-31-2020, 11:02 PM
RE: python dictionary - by pkmnmewtwo - Apr-01-2020, 12:38 AM
RE: python dictionary - by deanhystad - Apr-01-2020, 03:49 AM

Forum Jump:

User Panel Messages

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