Contents in Detailxv
Modifying Values in a Dictionary ................................ 94
Removing Key-Value Pairs ..................................... 96
A Dictionary of Similar Objects.................................. 96
Using get() to Access Values .................................... 97
Exercise 6-1: Person ........................................ 98
Exercise 6-2: Favorite Numbers ................................ 98
Exercise 6-3: Glossary ....................................... 99
Looping Through a Dictionary ......................................... 99
Looping Through All Key-Value Pairs .............................. 99
Looping Through All the Keys in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . 101
Looping Through a Dictionary’s Keys in a Particular Order.............. 102
Looping Through All Values in a Dictionary ........................ 103
Exercise 6-4: Glossary 2 .................................... 104
Exercise 6-5: Rivers ........................................ 105
Exercise 6-6: Polling ....................................... 105
Nesting........................................................ 105
A List of Dictionaries ........................................ 105
A List in a Dictionary ........................................ 108
A Dictionary in a Dictionary................................... 110
Exercise 6-7: People ....................................... 111
Exercise 6-8: Pets ......................................... 111
Exercise 6-9: Favorite Places ................................. 111
Exercise 6-10: Favorite Numbers .............................. 111
Exercise 6-11: Cities ....................................... 111
Exercise 6-12: Extensions .................................... 111
Summary ...................................................... 111
7
USER INPUT AND WHILE LOOPS 113
How the input() Function Works ....................................... 114
Writing Clear Prompts ....................................... 114
Using int() to Accept Numerical Input............................. 115
The Modulo Operator ....................................... 116
Exercise 7-1: Rental Car ..................................... 117
Exercise 7-2: Restaurant Seating ............................... 117
Exercise 7-3: Multiples of Ten . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Introducing while Loops ............................................ 117
The while Loop in Action ..................................... 117
Letting the User Choose When to Quit ............................ 118
Using a Flag.............................................. 120
Using break to Exit a Loop .................................... 121
Using continue in a Loop ..................................... 122
Avoiding Infinite Loops....................................... 122
Exercise 7-4: Pizza Toppings ................................123
Exercise 7-5: Movie Tickets .................................. 123
Exercise 7-6: Three Exits .................................... 123
Exercise 7-7: Infinity ....................................... 123
Using a while Loop with Lists and Dictionaries ............................. 124
Moving Items from One List to Another............................ 124
Removing All Instances of Specific Values from a List .................. 125
Filling a Dictionary with User Input .............................. 125
Exercise 7-8: Deli ......................................... 127