这是我应该编码的问题:
编写 showCast 函数的契约、文档字符串和实现,该函数接收电影标题并按字母顺序打印出给定电影中的角色及其对应的演员/女演员。列必须对齐(演员/女演员姓名前 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
我添加了三部电影,
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]) 只是为了确认它是列表中的一个大条目,根本没有分开])
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"
看起来您的功能运行正常showCast,但您可能需要调整一些内容才能使其按预期运行。我将指导您解决这些问题并向您展示如何修复它们。
showCast
aList = myIMDb[title]
您可以通过以下方式修改该showCast函数来实现此目的:
def showCast(title): if title in myIMDb: # Get the dictionary for the movie movie_cast = myIMDb[title] # Sort the characters alphabetically sorted_cast = sorted(movie_cast.items()) # Print header print(f"{'Character':<20} {'Actor/Actress':<20}") print("-" * 40) # Print each character and their corresponding actor for character, actor in sorted_cast: print(f"{character:<20} {actor:<20}") else: print("No such movie found")
movie_cast = myIMDb[title]
sorted_cast = sorted(movie_cast.items())
{'Character':<20} {'Actor/Actress':<20}
"-" * 40
"No such movie found"
>>> showCast("Shutter Island") Character Actor/Actress ---------------------------------------- Chuck Aule Mark Ruffalo Teddy Daniels Leonardo DiCaprio >>> showCast("Zombieland") Character Actor/Actress ---------------------------------------- Columbus Jesse Eisenberg Wichita Emma Stone
如果找不到影片,则会输出:
>>> showCast("Hairy Potter") No such movie found
myIMDb