If your teacher added you to a repl.it classroom or a Trinket course, login to your account to access the starter code for each exercise.
If you are NOT enrolled in a repl.it classroom or Trinket course, use the editors below to complete the project.
Note
The code editors embedded in the exercises all include a Remix button in the upper right corner which will save the code to your Trinket account. For the matching repl.it code, click these links:
The flavors
dictionary contains entries that pair different ice cream
flavors with their cost per scoop. Your job is to do the following:
return_cost
that takes a dictionary and flavor
choice as parameters.0
.return_cost
function, run the program and examine the
output. Make sure your function behaves as expected before moving to the
next step. Don’t forget to assign different strings to the choice
variable!fanciest_flavor
that takes a dictionary as a
parameter. The function should return the key name for the most expensive
choice in the dictionary.main()
, then run the program several
times and examine the output. Change the prices in flavors
after each
run to make sure your function correctly identifies the most expensive ice
cream flavor.Use the accumulator pattern to add new key/value pairs to an empty dictionary.
Write a function called assign_tickets
that takes a list of names as a
parameter.
The function should take each name from the list and use it to create a key in the dictionary.
For the value of each key, assign a random integer from 100-500.
Tip
We are not worried about giving two people the same ticket number in this exercise. However, for a real event we would want to prevent this.
Check out the Unique Dictionary Values
section in the Random Module
appendix if you would like to avoid
repeats in the values.
Return the new dictionary.
In main()
, call the assign_tickets
function and assign the result to
a ticket_holders
variable.
Print ticket_holders
to check that your code works as expected. Your output
should look something like:
{'Caleb': 192, 'Naomi': 490, 'Owen': 465, 'Ava': 248, 'Aaron': 421, 'Lydia': 306}
Oh no! Ticket numbers 100-199 were supposed to be held back for VIPs. You need to reassign tickets to anyone who was given one of the reserved seats. Use the editor in part B as you update your code.
Call the function fix_tickets
, and use ticket_holders
as the argument.
The fix_tickets
function should:
500
and reassign it
to the key.To check your code, be sure to print ticket_holders
before and after
calling the fix_tickets
function.
Sample Output:
Before: {'Caleb': 168, 'Naomi': 205, 'Owen': 193, 'Ava': 161, 'Aaron': 246, 'Lydia': 330}
After: {'Caleb': 668, 'Naomi': 205, 'Owen': 693, 'Ava': 661, 'Aaron': 246, 'Lydia': 330}
Write a function called character_count
that counts how many times each
character appears in a string.
The function should:
counts
.counts
, add it and
assign it a value of 1
.counts
, increase its value by
one.counts
dictionary and assign it to a results
variable in main()
.The counting should be case-insensitive. For example, 'a'
and 'A'
both count as the same letter.
Be sure to print the returned dictionary to check your code.
Tip
Here are some test strings and their results:
"Python ROCKS!"
returns {'p': 1, 'y': 1, 't': 1, 'h': 1, 'o': 2, 'n': 1, ' ': 1, 'r': 1, 'c': 1, 'k': 1, 's': 1, '!': 1}
."Balloons, bookkeepers, and bubbles."
returns {'b': 5, 'a': 2, 'l': 3, 'o': 4, 'n': 2, 's': 3, ',': 2, ' ': 3, 'k': 2, 'e': 4, 'p': 1, 'r': 1, 'd': 1, 'u': 1, '.': 1}
.Now display the character count result in a cleaner way. Update your code in the part D editor.
In the main()
function, loop through the results
dictionary and
print each key/value pair on its own line. For "B-A-L-L-O-O-N-S!"
, the
output would be:
The character counts for 'B-A-L-L-O-O-N-S!' are:
b: 1
-: 7
a: 1
l: 2
o: 2
n: 1
s: 1
!: 1
Be sure your output includes the introductory sentence.
Modify your code to print a key/value pair ONLY IF the character is a letter.
Modify your code again, but this time display the character counts in alphabetical order.
list
function to create a list of the keys from the
results
dictionary.The final output for "B-A-L-L-O-O-N-S!"
should be:
The character counts for 'B-A-L-L-O-O-N-S!' are:
a: 1
b: 1
l: 2
n: 1
o: 2
s: 1