Python Forum
Dictionary/List Homework
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary/List Homework
#11
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'
Reply
#12
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.
Reply
#13
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()
Reply
#14
You have a while loop on line 36, but there is nothing in the while loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#15
(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-...or-keyword
Reply
#16
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:')
Reply
#17
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#18
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.
Reply
#19
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.
Reply
#20
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'):.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with list homework eyal123 5 1,645 Nov-18-2022, 03:46 PM
Last Post: deanhystad
  Homework - List containing tuples containing dicti Men 4 2,023 Dec-28-2021, 12:37 AM
Last Post: Men
  Sorting list - Homework assigment ranbarr 1 2,235 May-16-2021, 04:45 PM
Last Post: Yoriz
  Loop through elements of list and include as value in the dictionary Rupini 3 2,654 Jun-13-2020, 05:43 AM
Last Post: buran
  How can details be dynamically entered into a list and displayed using a dictionary? Pranav 5 2,929 Mar-02-2020, 10:17 AM
Last Post: buran
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,044 Nov-21-2019, 10:06 PM
Last Post: ichabod801
  have homework to add a list to a list using append. celtickodiak 2 2,019 Oct-11-2019, 12:35 PM
Last Post: ibreeden
  Dictionary Homework beepBoop123 3 2,611 Dec-11-2018, 10:00 PM
Last Post: buran
  making a dictionary from a list, one key with multiple values in a list within a list rhai 4 3,607 Oct-24-2018, 06:40 PM
Last Post: LeSchakal
  Need some help with list and dictionary .txt GeekLife97 3 3,826 Jan-20-2017, 08:00 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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