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
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
create_monster_cast():
2 def
3
4
5
6 # This function adds the monsters to the dictionary
def add_cast_member (monsters, character, cast_member):
7
8
9
d1 = {}
return d1
dictionary = monsters
dictionary [character] = cast_member
return dictionary
10
11
12 # This function returns the name of cast member associated with the character
13 def get_cast_member (monsters, character):
14
if character not in monsters:
15
return
16
return monsters [character]
17
18 # This function returns the size of the dictionary
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
29
30
31
32
33
monsters = create_monster_cast()
34
35
36
monsters = add_cast_member(monsters, 'James P. "Sulley" Sullivan', "John Goodman")
monsters = add_cast_member (monsters, 'Michael "Mike" Wazowski', 'Bill Crystal')
monsters = add_cast_member (monsters, 'Boo', 'Mary Gibbs')
37
38 monsters = add_cast_member(monsters, 'Randall Boggs', 'Steve Buscemi')
39 monsters = add_cast_member(monsters,
'Randall Boggs', 'Steve Buscemi')
# 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
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
47
48
49
50 # Calling the change_cast_member function with new cast_member value
51 monsters = change_cast_member(monsters, "Mike", "Billy Crystal")
52 print("\nAfter changing the value\n\nMonster Cast : ')
53 for key in monsters.keys ():
54
55
56
57
print (format(key,"<30"),':',end='')
print (get_cast_member(monsters, key))
print_(format(key,"<30"),':', end='')
print(get_cast_member(monsters, key))](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F16d48e5d-c020-4b2c-adeb-ebc53a3fef14%2Ff03ebeed-9a5b-43a5-b856-62eecc4169e8%2F2fa7txg_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)