これが私がコーディングすることになっている質問です:
関数 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"