{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Day 5 - Exercise 3: The final exercise of the course" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "All you have to do is read the instructions, run all the notebook cells and fill in your answers when prompted." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The coded message:\n", "\n", "`\"{name} {maybeis} {chr(the_sum)} {the_snake} {word} {chr(the_length)}\"`\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Getting prepared - importing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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!\n", "\n", "Import a module called checker." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# write the import here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Verify that it worked:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checker.check()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Start up - defining and using variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a variable of type string, containing your name.\n", "\n", "The variable should be called `name`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Give the variable `name` as input to the function below, and run the cell:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checker.check_name(___)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As a warm up, show that you can not only work with strings, but also with numbers.\n", "\n", "Here's a number. Turn it into a float!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "number = '5'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# turn number into a float\n", "float_number = ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run this test:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checker.check_number(float_number)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Built-in functions and calculating mean values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some parts of the secret message turn out to rely upon how much code a specific person produces." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The list below `code_lines_per_day` shows how many lines of code this person writes per day.\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This represents the number of code lines a person writes per day\n", "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]\n", "\n", "# How many days are ther?\n", "the_length = ... # TODO change\n", "\n", "# How many lines did the person write?\n", "the_sum = ... # TODO change\n", "\n", "# How many lines of code on average?\n", "average = ... # TODO change\n", "\n", "print('The person wrote '+str(average)+' numbers of codes per day')\n", "checker.check_average(the_length, the_sum, average)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating and using dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the next part, you will work with dictionaries.\n", "\n", "Create a dictionary called `animals` with the keys `'snake'` and `'bird'`. All values should be the empty strings." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create the dictionary here\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then run this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checker.check_animals(animals)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set the value for `snake` to the string \"python\".\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Set the value for 'snake' to the string \"python\"\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And run the test:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checker.check_animals2(animals)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "the_snake = animals ... # Fix this line" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Test it:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checker.check_snake(the_snake)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### If statements, comparisons" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `checker` module stores a value called `x`.\n", "\n", "This is how to get the value of x:\n", "```py\n", "checker.x```\n", "\n", "Write an if statement that sets the value of `maybeis` according to the description:\n", "\n", "- If x is greater than (or equal to) 100, then maybeis should be \"will be\".\n", "\n", "- If x is between 100 and 60, then `maybeis` should be \"is\".\n", "\n", "- If x is smaller than (or equal to) 50, then `maybeis` should be \"is soon\". " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Put your if statement here:\n", "x = checker.x # store the value of checker.x in x\n", "maybeis = '' # set maybeis to an inintual value\n", "..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checker.check_maybe(maybeis)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Final task - files and loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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\n", "its correct filepath, ask an instructor.\n", "\n", "Open the file `pizza.txt` and read it line by line. For each line, print it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Open the file and read it line by line" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now modify your code above, so that for each line, you print only the first letter." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Loop over the file here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "word = ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Test that you are correct:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checker.check_word(word)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You have reached the end! The coded message from the top of the page is repeated here, now as a Python string. \n", "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)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# py3.6 or higher\n", "the_code = f\"{name} {maybeis} {chr(the_sum)} {the_snake} {word}{chr(the_length)}\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# lower versions\n", "the_code = \"{} {} {} {} {}{}\".format(name, maybeis, chr(the_sum), the_snake, word, chr(the_length))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ok, now let's print the result!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Run this code to see if you passed!\n", "checker.final(the_code)\n", "print(the_code)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.0" } }, "nbformat": 4, "nbformat_minor": 2 }