This is the final exercise for this Python programming course. The task is to decode the message below. This notebook will help you through it, step by step, and you can also check all your answers on the way. All you have to do is read the instructions, run all the notebook cells and fill in your answers when prompted.
The coded message:
"{name} {maybeis} {chr(the_sum)} {the_snake} {word} {chr(the_length)}"
Oh, that looks like a lot of python variables, right? Your job will be to find the right values for these! But as stated above, all you have to do is to follow the instructions.
There is a python script called checker.py
in the downloads
folder. It should be imported to this notebook, so that it can help you check your answers. Depending on how you run this notebook, you might have to move the file checker.py
so that it can be located by Python. Ask an instructor for help if you're not sure about this step!
Import a module called checker.
# write the import here
Verify that it worked:
checker.check()
Define a variable of type string, containing your name.
The variable should be called name
.
...
Give the variable name
as input to the function below, and run the cell:
checker.check_name(___)
As a warm up, show that you can not only work with strings, but also with numbers.
Here's a number. Turn it into a float!
number = '5'
# turn number into a float
float_number = ...
Run this test:
checker.check_number(float_number)
Some parts of the secret message turn out to rely upon how much code a specific person produces.
The list below code_lines_per_day
shows how many lines of code this person writes per day.
Fill in the slots below to calculate the number of days in the experiment, the total number of code lines written and the average number of lines per day.
# This represents the number of code lines a person writes per day
code_lines_per_day = [1, 1, 1, 2, 2, 3, 0, 1, 8, 0, 8, 6, 1, 1, 0, 1, 3, 3, 2, 5, 3, 0, 0, 7, 3, 7, 8, 5, 3, 8, 0, 3, 1]
# How many days are ther?
the_length = ... # TODO change
# How many lines did the person write?
the_sum = ... # TODO change
# How many lines of code on average?
average = ... # TODO change
print('The person wrote '+str(average)+' numbers of codes per day')
checker.check_average(the_length, the_sum, average)
For the next part, you will work with dictionaries.
Create a dictionary called animals
with the keys 'snake'
and 'bird'
. All values should be the empty strings.
# create the dictionary here
...
Then run this:
checker.check_animals(animals)
Set the value for snake
to the string "python".
# Set the value for 'snake' to the string "python"
...
And run the test:
checker.check_animals2(animals)
Finally, do you remember how to get values from a dictionary? Modify the code line below, so that the value for the key snake
is stored in the variable the_snake
.
the_snake = animals ... # Fix this line
Test it:
checker.check_snake(the_snake)
The checker
module stores a value called x
.
This is how to get the value of x:
checker.x```
Write an if statement that sets the value of `maybeis` according to the description:
- If x is greater than (or equal to) 100, then maybeis should be "will be".
- If x is between 100 and 60, then `maybeis` should be "is".
- If x is smaller than (or equal to) 50, then `maybeis` should be "is soon".
# Put your if statement here:
x = checker.x # store the value of checker.x in x
maybeis = '' # set maybeis to an inintual value
...
checker.check_maybe(maybeis)
The last test is on file reading. The file you will open is called pizza.txt
and is located in the downloads folder. If you're not sure how to get
its correct filepath, ask an instructor.
Open the file pizza.txt
and read it line by line. For each line, print it.
# Open the file and read it line by line
Now modify your code above, so that for each line, you print only the first letter.
# Loop over the file here.
When you're done, you should see a word printed on the screen (although every letter is on it's own line). What word is it? Save the answer in the variable word
.
word = ...
Test that you are correct:
checker.check_word(word)
You have reached the end! The coded message from the top of the page is repeated here, now as a Python string. Note that there are two lines, one for python 3.6 or higher, and one for lower versions. Run the cell that suits your Python version (or try both, if you're unsure).
# py3.6 or higher
the_code = f"{name} {maybeis} {chr(the_sum)} {the_snake} {word}{chr(the_length)}"
# lower versions
the_code = "{} {} {} {} {}{}".format(name, maybeis, chr(the_sum), the_snake, word, chr(the_length))
Ok, now let's print the result!
#Run this code to see if you passed!
checker.final(the_code)
print(the_code)