Python Forum
Dictionary/List Homework - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Dictionary/List Homework (/thread-14682.html)

Pages: 1 2 3


RE: Dictionary/List Homework - ImLearningPython - Dec-13-2018

Now that I have done that, I'm not sure how to fix this.
Traceback (most recent call last):
  File "C:/Users/brand/PycharmProjects/Test/playground/Playground.py", line 22, in <module>
    set_title = sorted(set(title_list))
TypeError: unhashable type: 'list'



RE: Dictionary/List Homework - nilamo - Dec-13-2018

Spin up the debugger, and see what title_list is, to try to find the issue.

python -m pdb myfile.py
-> # first line of program
(Pdb) 
Then type next (or just "n") to step through the code until you get to the line that's throwing an error. Once there, type interact, and then print(title_list).

If I were to take a guess, though, it's that title_list is a list of lists, which doesn't make sense to try to make a set out of. Remember, you just changed the structure of your data, so you'll probably need to change some of the code that parses that structure so it'll work with the new structure.


RE: Dictionary/List Homework - ImLearningPython - Dec-14-2018

I have a completely working program now with one exception. I can't seem to figure out where the indention error is coming from.
  File "C:/Users/brand/PycharmProjects/Test/Playground Two/.idea/Playground Two.py", line 38
    else:
    ^
IndentationError: expected an indented block

Process finished with exit code 1
movies = {2005: ['Munich', 'Steven Spielberg'],
        2006: ['The Prestige', 'Christopher Nolan','The Departed', 'Martin Scorsese'],
        2007: ['Into the Wild', 'Sean Penn'],
        2008: ['The Dark Knight', 'Christopher Nolan'],
        2009: ['Mary and Max', 'Adam Elliot'],
        2010: ['The King\'s Speech', 'Tom Hooper'],
        2011: ['The Artist', 'Michel Hazanavicius', 'The Help', 'Tate Taylor'],
        2012: ['Argo', 'Ben Affleck'],
        2013: ['12 Years a Slave', 'Steve McQueen'],
        2014: ['Birdman', 'Alejandro G. Inarritu'],
        2015: ['Spotlight', 'Tom McCarthy'],
        2016: ['The BFG', 'Steven Spielberg']}
# Prompt the user for a year
year = int(input())
print('Enter a year between 2005 and 2016:')

# Displaying the title(s) and directors(s) from that year
if year == 2005:
    print(movies[year][0]+', '+movies[year][1])
elif year == 2006:
    print(movies[year][0]+', '+movies[year][1]+'\n'+movies[year][2]+', '+movies[year][3])
elif year in range(2007,2010):
    print(movies[year][0]+', '+movies[year][1])
elif year == 2011:
   print(movies[year][0]+', '+movies[year][1]+'\n'+movies[year][2]+', '+movies[year][3])
elif year in range(2012, 2017):
    print(movies[year][0]+', '+movies[year][1])
else:
    print('N/A')
# Display menu

#MENU
options = input()
while options != 'q':
    if options != 'q' or 't' or 'd' or 'y':
        while options != 'q' or 'd' or 't' or 'y':
    else:
        if options == 'q':
            print('\nMENU\nSort by:\ny - Year\nd - Director\nt - Movie title\nq - Quit')
            print('\nChoose an option:')
        if options == 'y':
            print('\nMENU\nSort by:\ny - Year\nd - Director\nt - Movie title\nq - Quit')
            print('\nChoose an option:')
            for key in movies:
                print('%s:'% key)
                i = 0
                while i < len(movies[key]):
                    print('\t%s, %s'% (movies[key][i],movies[key][i +1]))
                    i += 2
                print()
        director_list = []
        if options == 'd':
            print('\nMENU\nSort by:\ny - Year\nd - Director\nt - Movie title\nq - Quit')
            print('\nChoose an option:')
            for key in movies:
                i = 1
                while i < len(movies[key]):
                    director_list.append(movies[key][i])
                    i += 2
            set_director = sorted(set(director_list))
            for director in set_director:
                print('%s:' % director)
                for year, value in sorted(movies.items()):
                    if director in value:
                        print('\t%s, %s' % (value[value.index(director) - 1], year))
                print()
        title_list = []
        if options == 't':
            print('\nMENU\nSort by:\ny - Year\nd - Director\nt - Movie title\nq - Quit')
            print('\nChoose an option:')
            for key in movies:
                i = 0
                while i < len(movies[key]):
                    title_list.append(movies[key][i])
                    i += 2
            set_title = sorted(set(title_list))
            for title in set_title:
                print('%s:' % title)
                for year, value in sorted(movies.items()):
                    if title in value:
                        print('\t%s, %s' % (value[value.index(title)+1], year))
                print()



RE: Dictionary/List Homework - ichabod801 - Dec-14-2018

You have a while loop on line 36, but there is nothing in the while loop.


RE: Dictionary/List Homework - nilamo - Dec-14-2018

(Dec-14-2018, 02:53 PM)ImLearningPython Wrote: if options != 'q' or 't' or 'd' or 'y':
Furthermore, that line is NOT doing what you think it's doing.
Explanation can be found here: https://python-forum.io/Thread-Multiple-expressions-with-or-keyword


RE: Dictionary/List Homework - ImLearningPython - Dec-14-2018

I am now getting my inside loops to run indefinitely, why?
If I don't have the while loop at the beginning the other loops run as they should and stop. When I add the while and else statement, they run non-stop.

options = input()
while options == 'y' or 'd' or 't':
    if options == 'y':
        print('\nMENU\nSort by:\ny - Year\nd - Director\nt - Movie title\nq - Quit')
        print('\nChoose an option:')
        for key in movies:
            print('%s:' % key)
            i = 0
            while i < len(movies[key]):
                print('\t%s, %s' % (movies[key][i], movies[key][i + 1]))
                i += 2
            print()
    director_list = []
    if options == 'd':
        print('\nMENU\nSort by:\ny - Year\nd - Director\nt - Movie title\nq - Quit')
        print('\nChoose an option:')
        for key in movies:
            i = 1
            while i < len(movies[key]):
                director_list.append(movies[key][i])
                i += 2
        set_director = sorted(set(director_list))
        for director in set_director:
            print('%s:' % director)
            for year, value in sorted(movies.items()):
                if director in value:
                    print('\t%s, %s' % (value[value.index(director) - 1], year))
            print()
    title_list = []
    if options == 't':
        print('\nMENU\nSort by:\ny - Year\nd - Director\nt - Movie title\nq - Quit')
        print('\nChoose an option:')
        for key in movies:
            i = 0
            while i < len(movies[key]):
                title_list.append(movies[key][i])
                i += 2
        set_title = sorted(set(title_list))
        for title in set_title:
            print('%s:' % title)
            for year, value in sorted(movies.items()):
                if title in value:
                    print('\t%s, %s' % (value[value.index(title) + 1], year))
            print()
else:
    if options == 'q':
        print('\nMENU\nSort by:\ny - Year\nd - Director\nt - Movie title\nq - Quit')
        print('\nChoose an option:')



RE: Dictionary/List Homework - ichabod801 - Dec-14-2018

That's because your first while loop's condition is always True. This is what nilamo was saying in his last post. You need to follow the link in that post.


RE: Dictionary/List Homework - ImLearningPython - Dec-14-2018

I'm looking at that link to try and get a better understanding of loops, I struggle greatly with them.
I posted my post when he posted his, I did not see it while I was typing mine. Once I seen it, I went to the link and I'm trying to figure it out.

Thank you for your help. I do enjoy this, slow learner, yet still enjoyable non the less.


RE: Dictionary/List Homework - ImLearningPython - Dec-14-2018

If I am understanding this correctly. In this line of code I would need to change options to something else. Then make options to this in place of 'yes'.
yes = ("Yes", "Y", "yes", "y")
if value in yes:
options = input()
while options != 'q':
    if options != 'q' or 't' or 'd' or 'y':
        while options != 'q' or 'd' or 't' or 'y':
            if options == 'y':
                print('\nMENU\nSort by:\ny - Year\nd - Director\nt - Movie title\nq - Quit')
                print('\nChoose an option:')
                for key in movies:
                    print('%s:' % key)
                    i = 0
                    while i < len(movies[key]):
                        print('\t%s, %s' % (movies[key][i], movies[key][i + 1]))
                        i += 2
                    print()
            director_list = []
            if options == 'd':
                print('\nMENU\nSort by:\ny - Year\nd - Director\nt - Movie title\nq - Quit')
                print('\nChoose an option:')
                for key in movies:
                    i = 1
                    while i < len(movies[key]):
                        director_list.append(movies[key][i])
                        i += 2
                set_director = sorted(set(director_list))
                for director in set_director:
                    print('%s:' % director)
                    for year, value in sorted(movies.items()):
                        if director in value:
                            print('\t%s, %s' % (value[value.index(director) - 1], year))
                    print()
            title_list = []
            if options == 't':
                print('\nMENU\nSort by:\ny - Year\nd - Director\nt - Movie title\nq - Quit')
                print('\nChoose an option:')
                for key in movies:
                    i = 0
                    while i < len(movies[key]):
                        title_list.append(movies[key][i])
                        i += 2
                set_title = sorted(set(title_list))
                for title in set_title:
                    print('%s:' % title)
                    for year, value in sorted(movies.items()):
                        if title in value:
                            print('\t%s, %s' % (value[value.index(title) + 1], year))
                    print()
            if options == 'q':
                print('\nMENU\nSort by:\ny - Year\nd - Director\nt - Movie title\nq - Quit')
                print('\nChoose an option:')

 
So far I have been able to get the output to be what I want. I just can't seem to get it to loop and offer another input till user inputs q.


RE: Dictionary/List Homework - ichabod801 - Dec-14-2018

You haven't fixed the problem, you just created more of it. Lines 3 and 4 still have the same problem of 'or's without operators. And I'm not sure why they are there anyway. You should have one loop, with a while condition using the in operator. Note that you case-insensitive checks by using lower to lower-case the input. So just make one while loop with while options.lower() in ('y', 'd', 't'):.