|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Lecture I: EXERCISES" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "## Exercise 1:" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "Write a program that asks the user for an integer and then print (in one line) if the integer is divisible by 2, divisible by 5, both or neither.\n", |
| 22 | + "\n", |
| 23 | + "Examples:\n", |
| 24 | + "- Input: 32 Output: \"32 is divisible by 2\" \n", |
| 25 | + "- Input: 25 Output: \"25 is divisible by 5\"\n", |
| 26 | + "- Input: 40 Output: \"40 is divisible by 2 and 5\"\n", |
| 27 | + "- Input: 77 Output: \"77 is not divisible by 2 or 5\"" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "### Possible Solution:" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 7, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [ |
| 42 | + { |
| 43 | + "name": "stdout", |
| 44 | + "output_type": "stream", |
| 45 | + "text": [ |
| 46 | + "Choose an integer: 77\n", |
| 47 | + "77 is not divisible by 2 or 5\n" |
| 48 | + ] |
| 49 | + } |
| 50 | + ], |
| 51 | + "source": [ |
| 52 | + "x = int(input(\"Choose an integer: \"))\n", |
| 53 | + "if x%2 == 0:\n", |
| 54 | + " if x%5 == 0:\n", |
| 55 | + " print(f\"{x} is divisible by 2 and 5\")\n", |
| 56 | + " else:\n", |
| 57 | + " print(f\"{x} is divisible by 2\")\n", |
| 58 | + "elif x%5 == 0:\n", |
| 59 | + " print(f\"{x} is divisible by 5\")\n", |
| 60 | + "else:\n", |
| 61 | + " print(f\"{x} is not divisible by 2 or 5\")" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "markdown", |
| 66 | + "metadata": {}, |
| 67 | + "source": [ |
| 68 | + "This solution uses always two comparisons, regardless of the input (remember that `elif` is only executed when the `if` before is `false` and that an `if` inside an `if` is only executed when the first `if` is `true`)." |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "markdown", |
| 73 | + "metadata": {}, |
| 74 | + "source": [ |
| 75 | + "## Exercise 2:" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "markdown", |
| 80 | + "metadata": {}, |
| 81 | + "source": [ |
| 82 | + "Write a program that asks the user for their name, age, favorite number and the number they hate the most. \n", |
| 83 | + "\n", |
| 84 | + "Save the data to a list (this could be done without saving the data to a list, but I want you to practice with lists) and print the following: the last letter of their name, if their age is equal or over 18 print \"you are an adult\" if not \"you are a minor\", if their favorite number is bigger than their most hated number print \"your favorite number is bigger than your hated number\" if it is smaller print \"your favorite number is smaller than your hated number\" and if they are equal print \"you hate your favorite number\".\n", |
| 85 | + "\n", |
| 86 | + "Example: \n", |
| 87 | + "- Input: John, 43, 7, 2 Output: \"The last letter of your name is n. You are an adult. Your favorite number is bigger than your hated number\"" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "markdown", |
| 92 | + "metadata": {}, |
| 93 | + "source": [ |
| 94 | + "### Possible Solution:" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": 11, |
| 100 | + "metadata": {}, |
| 101 | + "outputs": [ |
| 102 | + { |
| 103 | + "name": "stdout", |
| 104 | + "output_type": "stream", |
| 105 | + "text": [ |
| 106 | + "What is your name: John\n", |
| 107 | + "What is your age: 43\n", |
| 108 | + "What is your favorite number: 7\n", |
| 109 | + "What number you hate the most: 2\n", |
| 110 | + "The last letter of your name is n. You are an adult. Your favorite number is bigger than your hated number.\n" |
| 111 | + ] |
| 112 | + } |
| 113 | + ], |
| 114 | + "source": [ |
| 115 | + "data_list = [] #Create an empty list\n", |
| 116 | + "\n", |
| 117 | + "data_list.append(input(\"What is your name: \"))\n", |
| 118 | + "data_list.append(int(input(\"What is your age: \")))\n", |
| 119 | + "data_list.append(int(input(\"What is your favorite number: \")))\n", |
| 120 | + "data_list.append(int(input(\"What number you hate the most: \")))\n", |
| 121 | + "\n", |
| 122 | + "age = \"an adult\" if data_list[1]>=18 else \"a minor\" #You can do ifs in one line like this\n", |
| 123 | + "\n", |
| 124 | + "if data_list[2]>data_list[3]:\n", |
| 125 | + " n = \"Your favorite number is bigger than your hated number\"\n", |
| 126 | + " \n", |
| 127 | + "elif data_list[2]<data_list[3]:\n", |
| 128 | + " n = \"Your favorite number is smaller than your hated number\"\n", |
| 129 | + "else:\n", |
| 130 | + " n = \"You hate your favorite number\"\n", |
| 131 | + " \n", |
| 132 | + "print(f\"The last letter of your name is {data_list[0][-1]}. You are {age}. {n}.\")" |
| 133 | + ] |
| 134 | + } |
| 135 | + ], |
| 136 | + "metadata": { |
| 137 | + "kernelspec": { |
| 138 | + "display_name": "Python 3", |
| 139 | + "language": "python", |
| 140 | + "name": "python3" |
| 141 | + }, |
| 142 | + "language_info": { |
| 143 | + "codemirror_mode": { |
| 144 | + "name": "ipython", |
| 145 | + "version": 3 |
| 146 | + }, |
| 147 | + "file_extension": ".py", |
| 148 | + "mimetype": "text/x-python", |
| 149 | + "name": "python", |
| 150 | + "nbconvert_exporter": "python", |
| 151 | + "pygments_lexer": "ipython3", |
| 152 | + "version": "3.6.9" |
| 153 | + } |
| 154 | + }, |
| 155 | + "nbformat": 4, |
| 156 | + "nbformat_minor": 2 |
| 157 | +} |
0 commit comments