xercise: Check Monster Character Exists Description In this series of exercises, you will create functions to create, modify and examine dictionaries that represent characters in an animated film, and the cast members who voice the characters. The keys of the dictionary will be character names. The values in the dictionary will be voice actor names. For this exercise, you will create a function that checks if a character is already in the dictionary. Files monsterfunctions.py : set of functions to work with monster cast dictionaries. Function Name has_character Parameters monsters: a dictionary character: a string, the name of a character Action Checks if character is a key in monsters. Return Value True if character is a key in monsters, otherwise False. Examples monsters = create_monster_cast() monsters = add_cast_member(monsters, "Mike", "William Crystal") has_character(monsters, "Mike") -> True has_character(monsters, "Sully") -> False
Exercise: Check Monster Character Exists
Description
In this series of exercises, you will create functions
to create, modify and examine dictionaries that
represent characters in an animated film, and the
cast members who voice the characters. The
keys of the dictionary will be character names.
The values in the dictionary will be voice actor
names.
For this exercise, you will create a function that
checks if a character is already in the dictionary.
Files
- monsterfunctions.py : set of functions to work with monster cast dictionaries.
Function Name
has_character
Parameters
- monsters: a dictionary
- character: a string, the name of a character
Action
Checks if character is a key in monsters.
Return Value
True if character is a key in monsters, otherwise False.
Examples
monsters = create_monster_cast()
monsters = add_cast_member(monsters, "Mike", "William Crystal")
has_character(monsters, "Mike") -> True
has_character(monsters, "Sully") -> False
![1 #This function creates a dictionary
2 def create_monster_cast():
d1 = ()
return di
4
5
6 # This function adds the monsters to the dictionary
7
def add_cast_member (monsters, character, cast_member):
8
9
10 return dictionary
11
12 # This function returns the name of cast member associated with the character
13 def get_cast_member(monsters, character):
14
15
dictionary = monsters
dictionary [character] = cast member
if character not in monsters:
return **
return monsters [character]
16
17
18 #This function returns the size of the dictionary
29
30
31
32
33
34
19 # i.e. number of character present in the dictionary
20 def get_cast_size(monsters):
21
return len(monsters)
22
23 # Definition of the function to change the cast member of a character
24 def change_cast_member (monsters, character, cast_member):
25
26
27
28
#This statement changes the cast member
# If the character is not available in the dictionary, then it creates it
monsters [character] = cast member
# return the monsters dictionary
return monsters
monsters = create_monster_cast()
35
monsters = add_cast_member(monsters, 'James P. "Sulley" Sullivan", "John Goodman")
36 monsters = add_cast_member (monsters, 'Michael "Mike" Wazowski", "Bill Crystal")
37 monsters = add_cast_member(monsters, 'Boo', 'Mary Gibbs')
38 monsters = add_cast_member(monsters, 'Randall Boggs', 'Steve Buscemi')
39 monsters = add_cast_member(monsters,
'Randall Boggs', 'Steve Buscemi')
40 monsters = add_cast_member(monsters, "Henry J. Waternoose III", "James Coburn")
41 monsters = add_cast_member(monsters, "Mike", "William Crystal")
42
43
44 print('Monster Cast: ")
45 for key in monsters.keys():
46](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F8e2867a3-7607-4e4a-9d3d-50b07008b50c%2F09a72e8a-c558-4347-9fb2-7e85391b46b0%2Fewlhpoa_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 4 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)