Python Forum
Dictonary and list help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictonary and list help
#3
This should be enough to run it.

So the goal is to organize the list of movies alphabetically by title, while matching up the director and the year the film was made.

names = ['Munich, Steven Spielberg', 'The Prestige, Christopher Nolan' , 'The Departed, Martin Scorsese' ,
'Into the Wild, Sean Penn' , 'The Dark Knight, Christopher Nolan' , 'Mary and Max, Adam Elliot' , "The King's Speech, Tom Hooper" , 
'The Artist, Michel Hazanavicius' , 'The Help, Tate Taylor' , 'Argo, Ben Affleck' , '12 Years a Slave, Steve McQueen' , 
'Birdman, Alejandro G. Inarritu' ,'Spotlight, Tom McCarthy' , 'The BFG, Steven Spielberg']
movies = {
2005 : names[0], 2006 : names[1:3], 2007 : names[3], 2008 : names[4], 2009 : names[5], 2010 : names[6], 
2011 : names[7:9], 2012 : names[9], 2013 : names[10], 2014 : names[11], 2015 : names[12], 2016 : names[13] }

# Prompt the user for a year 
year = input('Enter a year between 2005 and 2016:\n')
year = int(year)

# Displaying the title(s) and directors(s) from that year

if  year == 2006 or year == 2011:
    temp = movies[year]
    print(temp[0])
    print(temp[1])
#To diplay years with multiple movies
elif year == 2005 or 2006 < year < 2011:
    print(movies[year])
elif 2011 < year < 2017:
    print(movies[year])
else:
    print('N/A')
print('')

# Display menu
print('MENU')
print('Sort by:')
print('y - Year')
print('d - Director')
print('t - Movie title')
print('q - Quit')
print('')
# Carry out the desired option: Display movies by year, 
sel = input('Choose an option:\n')
while sel == 'y' or sel == 'd' or sel == 't' or sel == 'q':
    
    if sel == 'q':
        break
# Sort movies by year
    elif sel == 'y':
        for year in movies.keys():
            year1 = str(year)
            print(year1 + ':')
            if year == 2006 or year == 2011:
                temp = movies[year]
                print('\t' + temp[0])
                print('\t' + temp[1])
                print('')
            else:
                print('\t'+ movies[year])
                print('')
                
#sort by title  (Having trouble matching the year)
    elif sel == 't':
        names.sort()
        for name in names:
            for key, value in movies.items():
                #print('Test test tes',name)
                if name == 'The Prestige, Christopher Nolan':
                    year = 2006
                elif name == 'The Departed, Martin Scorsese':
                    year = 2006
                elif name == 'The Help, Tate Taylor' or name == 'The Artist, Michel Hazanavicius':
                    year = 2011
                elif name == value:
                    year = key
            temp = name.find(',')
            print(name[0:temp] + ':' )
            print('\t' + name[temp + 2:len(name)] + ',' , year) 
            print('')
 
I know the if,else statement isn't the best way to go about this.

if name == 'The Departed' or name == 'The Prestige':
                    year = 2006
                elif name == 'The Help' or name == 'The Artist':
                    year = 2011
                elif name == value:
                    year = key
This is just so I had working code to turn in
Reply


Messages In This Thread
Dictonary and list help - by Btoulouse - Dec-09-2018, 08:44 PM
RE: Dictonary and list help - by Larz60+ - Dec-09-2018, 09:52 PM
RE: Dictonary and list help - by Btoulouse - Dec-09-2018, 10:01 PM
RE: Dictonary and list help - by Larz60+ - Dec-10-2018, 10:52 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Find a substring in a dictonary; value substring of key aapurdel 2 7,083 May-31-2019, 06:14 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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