0

これが私がコーディングすることになっている質問です:

関数 showCast のコントラクト、docstring、および実装を記述します。この関数は、映画のタイトルを取得し、指定された映画の対応する俳優/女優を文字のアルファベット順に出力します。列は整列する必要があります (俳優/女優の名前の前に 20 文字 (キャラクターの名前を含む))。映画が見つからない場合は、エラー メッセージが出力されます。

ここで何が起こるべきかの例を示します

>>> 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



>>> showCast('Hairy Potter')

No such movie found

以下は、おそらく質問に答えるのに役立つ、同じプロジェクト用に私が書いた他の関数です。これまでに行ったことの要約は、映画のタイトルのキーと別の辞書の値を持つ、myIMDb という名前の辞書を作成することです。その辞書ではそのキーは映画の登場人物であり、値は俳優です。そして、私はそれで何かをしました。myIMDb はレコードのグローバル変数です。

他の関数、それらが行うことはdocStringです

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

ムービーを3つ追加しました。

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"

今困っているところ

showCast関数を書くことになっているのですが、なかなか手こずっています。私はしばらくそれをいじっていましたが、 myIMDb.values() を呼び出すとすべてが返されます。そして、それらを並べ替えてテーブルを作成するためにループすることはできないようです。

これが私がこれまでに思いついたことですが、私が望んでいたことはしません。あなたの一人が私を正しい方向に導くことができることを願っています。(コメントアウトされた領域は、私が以前に行っていたもので、私の一連の考えを見ることができます。[print(alist) と print(alist[0]) は、それがリスト内の 1 つの大きなエントリであることを確認するためのものであり、まったく分離されている])

def showCast(title):

    if title in myIMDb:
        actList=[]
        chList=[]
        aList = myIMDb[title]
        print (aList)

          """"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"
4

2 に答える 2

0

まず、addMovie関数で何も返すべきではないと思います。それをグローバル変数に追加するだけです:

myIMDb = {}

def addMovie (title, charList, actList):
    global myIMDb

    """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

グローバル変数を頻繁に使用することはお勧めしませんが、この場合は許容できると思います:D

その後、あなたのshowCast関数で、私はこれを使用します:

def showCast(title):
    if title in myIMDb:
        actList=[]
        chList=[]
        movie = myIMDb[title]
        for character, cast in movie.keys(), movie.values(): #grab the character from
        #the keys, and cast from the values. 
              chList.append(character)
              actList.append(cast)

        print (chList, actList)
    else:
        return "Movie not Found"

ここに私の出力があります:

['Columbus', 'Jesse Eisenberg, '] ['Wichita', 'Emma Stone']

期待どおりに動作しています。これが役立つことを願っています!

于 2013-10-25T00:04:09.220 に答える
-1

多分これらのスニペットはあなたを助けるでしょう:

テーブル行の印刷

def printRow(character, actor):
    print(character + (20 - len(character)) * ' ' + actor))

文字の並べ替え

def getSortedTitleList(titleList):
    sortedList = []
    characters = sorted(titleList)
    for character in characters:
        sortedList.append((character, titleList[character]))
    return sortedList

注: Python 3 と互換性があるように変更dict.keys().sort()しました。sorted(dict)

于 2013-10-24T23:53:56.883 に答える