Activities: Week 4¶
You have the following graded activities:
- Before Tuesday’s class:
- Read Dictionaries
- Review reading about Understanding Code
- Try the exercises included within each chapter section.
- Read Indefinite Iteration and try the exercises in the sections.
- Before Thursday’s class:
- Read Dictionary Accumulation and try the exercises
- Read Defining Functions, and try the exercises in that chapter
- Before Sunday at 11:59 PM:
- Save answers to each of the exercises in Problem Set 3 and submit your Demonstrate Your Understanding assignment to Canvas.
Problem Set¶
Autumn is interchangeably known as fall in the US and Canada, and is one of the four temperate seasons. Autumn marks the transition from summer into winter. Some cultures regard the autumn equinox as mid autumn while others, with a longer temperature lag, treat it as the start of autumn then. In North America, autumn starts with the September equinox, while it ends with the winter solstice. (Wikipedia)
Below is a dictionary
diction
with two key-value pairs inside it. The string "python"
is one of its keys. Using dictionary mechanics, print out the value of the key "python"
.
Here’s another dictionary, nd
.
PART 1
Write code to print out each key-value pair in it, one key and its value on each line. Your output should look somewhat like this (remember, the order may be different!):
autumn spring
4 seasons
23 345
well spring
PART 2
Then, write code to increase the value of key "23"
by 5.
PART 3
Finally, write code to print the value of the key "well"
. Your code should work no matter what the value of the key “well” is.
HINTS:
- Printing things with a comma, e.g. print("hello", "everyone")
will print out those things on the same line with a space in between them: hello everyone
.
- Your code should work no matter what the values corresponding to the keys are!
Below is an empty dictionary saved in the variable
nums
, and a list saved in the variable num_words
. Use iteration and dictionary mechanics to add each element of num_words
as a key in the dictionary nums
. Each key should have the value 0
. The dictionary should end up looking something like this when you print it out (remember, you can’t be sure of the order): {"two":0,"three":0,"four":0,"eight":0,"seventeen":0,"not_a_number":0}
Write code that will keep printing what the user inputs over and over until the user enters the string “quit”.
Given the string
s
in the code below, write code to figure out what the most common word in the string is and assign that to the variable abc
. (Do not hard-code the right answer.) Hint: dictionary mechanics and dictionary accumulation will be useful here.
Take a look at the code below. The function
subtract_five
is supposed to take one integer as input and return that integer minus 5. You’ll get an error if you run it as is. Change the function so it works and passes the test!
Define a function called
change_amounts
that takes one integer as input. If the input is larger than 10, it should return the input + 5. If the input is smaller than or equal to 10, it should return the input + 2.