Note that there are some explanatory texts on larger screens.

plurals
  1. POPrinting a Table from a Dictionary
    primarykey
    data
    text
    <p>Here's the question that I'm supposed to code for:</p> <blockquote> <p>Write the contract, docstring and implementation for a function showCast that takes a movie title and prints out the characters with corresponding actors/actresses from the given movie in an alphabetical order of characters. The columns must be aligned (20 characters (including the character's name) before the name of the actor/actress.) If the movie is not found, it prints out an error message.</p> </blockquote> <p>It gives an example of what's supposed to happen here</p> <pre><code>&gt;&gt;&gt; showCast("Harry Potter and the Sorcerer's Stone") Character Actor/Actress ---------------------------------------- Albus Dumbledore Richard Harris Harry Potter Daniel Radcliffe Hermione Granger Emma Watson Ron Weasley Rupert Grint &gt;&gt;&gt; showCast('Hairy Potter') No such movie found </code></pre> <p>Here are other functions that I've written for the same project that will probably be of assistance in answering the question. A summary of what I've had to do so far is that I'm creating a dictionary, called myIMDb, with a key of the title of the movie, and the value another dictionary. In <em>that</em> dictionary that key is a character of a movie, and the value is the actor. And I've done stuff with it. myIMDb is a global variable for the record.</p> <p>Other functions, what they do is the docString</p> <pre><code>def addMovie (title, charList, actList): """The function addMovie takes a title of the movie, a list of characters, and a list of actors. (The order of characters and actors match one another.) The function addMovie adds a pair to myIMDb. The key is the title of the movie while the value is a dictionary that matches characters to actors""" dict2 = {} for i in range (0, len(charList)): dict2 [charList[i]] = actList[i] myIMDb[title] = dict2 return myIMDb </code></pre> <p>I've added three movies, </p> <pre><code>addMovie("Shutter Island", ["Teddy Daniels", "Chuck Aule"],["Leonardo DiCaprio, ","Mark Ruffalo"]) addMovie("Zombieland", ["Columbus", "Wichita"],["Jesse Eisenberg, ","Emma Stone"]) addMovie("O Brother, Where Art Thou", ["Everett McGill", "Pete Hogwallop"],["George Clooney, ","John Turturro"]) def listMovies(): """returns a list of titles of all the movies in the global variable myIMDb""" return (list(myIMDb.keys())) def findActor(title, name): """ takes a movie title and a character's name and returns the actor/actress that played the given character in the given movie. If the given movie or the given character is notfound, it prints out an error message""" if title in myIMDb: if name in myIMDb[title]: return myIMDb[title][name] else: return "Error: Character not in Movie" else: return "Error: No movie found" </code></pre> <p><strong>Now where I'm having trouble</strong></p> <p>I'm supposed to write the showCast function, but I'm having a lot of trouble. I've been tinkering with it for a while but when I call myIMDb.values() everything returns. And I can't seem to loop through it to sort them to create the table. </p> <p>Here's what I've come up with so far, but it doesn't do what I was hoping. I'm just hoping that one of you can steer me in the right direction. (The commented out region is what I was doing before, just so you can see my train of thought. [the print(alist) and print(alist[0]) was just to confirm that it's one big entry in a list, not separated at all])</p> <pre><code>def showCast(title): if title in myIMDb: actList=[] chList=[] aList = list(myIMDb.values()) print (aList) print (aList[0]) """"for i in range (len(aList)): if i%2==0: chList.append(aList[i]) else: actList.append(aList[i]) print(chList) print(actList)"""" else: return "Movie not Found" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload