{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Additional Exercises - Week 1\n",
"## Variables, Input, Output, if-elif-else\n",
"\n",
"If you are eager to practise a little more, here are some exercises.\n",
"\n",
"### Personal data\n",
"Get the personal data (name, firstname, city, ...) of a person using `input()`. Output a statement, which summarizes the input. Example:\n",
"\n",
" What is your name: Jacobs\n",
" What is your firstname: Stephan\n",
" Where do you live: Aachen\n",
" Stephan Jacobs lives in Aachen\n",
" \n",
"#### Hint\n",
"This task requires the usage of `input()`. The input should be written into three variables. Finally, the output with `print()` requires the input of several parameters, that means the three variables and an additional text. The parameters have to be seperated by commas."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"name = input(\"What is your name: \")\n",
"firstname = input(\"What is your firstname: \")\n",
"city = input(\"Where do you live: \")\n",
"\n",
"print(firstname, name, \"lives in\", city)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Rectangular cuboid\n",
"A rectangular [cuboid](https://en.wikipedia.org/wiki/Cuboid) is a six-faced geometrical object. All angles are right angles and the edges can have three different lengths *a, b, c*. The Volume of the cuboid can be calculated with the formula:\n",
"\n",
" vol = a * b * c\n",
" \n",
"The surface of the cuboid can be calculated with the formula:\n",
"\n",
" sur = 2 * (a * b + a * c + b * c)\n",
" \n",
"Write a program, that asks for the length of `a`, `b` and `c` and outputs the volume and the surface of the cuboid. Example:\n",
"\n",
" What is the length of the first edge: 3\n",
" What is the length of the second edge: 4\n",
" What is the lenght of the third edge: 5\n",
" The cuboid has a volume of 60 and a surface of 94\n",
" \n",
"#### Hint\n",
"Again, three values have to be read through `input()`. This time, the values are of data type integer (or float). Thus, the input has to be converted using the function `int()` (or `float()`). The formula for the calculation can be typed in as it is shown in the exercise."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = int(input(\"What is the length of the first edge: \"))\n",
"b = int(input(\"What is the length of the second edge: \"))\n",
"c = int(input(\"What is the length of the thrid edge: \"))\n",
"\n",
"vol = a * b * c\n",
"sur = 2 * (a * b + a * c + b * c)\n",
"\n",
"print(\"The cuboid has a volume of\", vol, \"and a surface of\", sur)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Window\n",
"The window should be open if it is warm and if it is not raining. Get the information if it is warm and if it is raining by `input()`. Then output if the window shall be open or closed. Example:\n",
"\n",
" Is it warm? y\n",
" Does it rain? n\n",
" The window should be open.\n",
" \n",
" Is it warm? y\n",
" Does it rain? y\n",
" The window should be closed.\n",
" \n",
"#### Hint\n",
"The challenge with this task is, to get a *boolean* Value to control the `if` statement. Again, `input()` is used to get an input. The user is expected to answer with either \"y\" or \"n\". \n",
"\n",
"- In the first solution the values stored in the variables `warm` and `rain` are compared with predefined values within the `if` statement,\n",
"- In the second solution the variables `warm` and `rain` are boolean variables. This makes the `if` statement better readable. However, to assign values to these variables, two more `if` statements are required.\n",
"- In the last solution, the assignment of the variables is more compact. However, it might be a bit hard to read.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Solution 1\n",
"warm = input(\"Is it warm? \")\n",
"rain = input(\"Does it rain? \")\n",
"\n",
"if warm == \"y\" and rain != \"y\":\n",
" print(\"The window should be open.\")\n",
"else:\n",
" print(\"The window should be closed.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Solution 2\n",
"answer = input(\"Is it warm? \")\n",
"if answer == \"y\":\n",
" warm = True\n",
"else:\n",
" warm = False\n",
"\n",
"answer = input(\"Does it rain? \")\n",
"if answer == \"y\":\n",
" rain = True\n",
"else:\n",
" rain = False\n",
"\n",
"if warm and not (rain):\n",
" print(\"The window should be open.\")\n",
"else:\n",
" print(\"The window should be closed.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Solution 3\n",
"answer = input(\"Is it warm? \")\n",
"warm = answer == \"y\"\n",
"\n",
"answer = input(\"Does it rain? \")\n",
"rain = answer == \"y\"\n",
"\n",
"if warm and not (rain):\n",
" print(\"The window should be open.\")\n",
"else:\n",
" print(\"The window should be closed.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Celsius and Fahrenheit and Kelvin\n",
"There are different scales for temperature. To convert the temperatures there are the following formulas:\n",
"\n",
" Celsius = 5/9 * (Fahrenheit - 32)\n",
" Celsius = Kelvin - 273.15\n",
" \n",
"Write a program, that takes Celsius as input and outputs the values in Fahrenheit and Kelvin.\n",
"\n",
" What is the temperature in Celsius: 25\n",
" 25 Celsius is the same as 77.0 Fahrenheit\n",
" 25 Celsius is the same as 298.15 Kelvin\n",
" \n",
"#### Hint\n",
"Again, take a value as input and first convert it into an integer (or a float). Of course, you have to prepare the calculation first, to derive the Fahrenheit and Kelvin from the Celsius."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cel = int(input(\"What is the temperature in Celsius: \"))\n",
"\n",
"fah = cel * 9 / 5 + 32\n",
"kel = cel + 273.15\n",
"\n",
"print(cel, \"Celsius is the same as\", fah, \"Fahrenheit\")\n",
"print(cel, \"Celsius is the same as\", kel, \"Kelvin\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Calulate the mean\n",
"Using `input()` get five numbers and calculate the mean of these values. Example:\n",
"\n",
" First number: 10\n",
" Second number: 20\n",
" Third number: 15\n",
" Fourth number: 23\n",
" Fifth number: 18\n",
" The mean is: 17.2\n",
"\n",
"#### Hint\n",
"The program is acutally quite easy: Get all the input and do two easy calculations. However, it points to the next week: This way of working is not flexible, the program only works with five values. Not with four, not with six. In addition, it requires some repetition, the same statement (with little variation) is used again and again. What requirements with respect to the Python can be derived due to this shortcomings?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n1 = int(input(\"First number: \"))\n",
"n2 = int(input(\"Second number: \"))\n",
"n3 = int(input(\"Third number: \"))\n",
"n4 = int(input(\"Fourth number: \"))\n",
"n5 = int(input(\"Fifth number: \"))\n",
"\n",
"sum_n = n1 + n2 + n3 + n4 + n5\n",
"mean = sum_n / 5\n",
"\n",
"print(\"The mean is:\", mean)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Sort three numbers\n",
"Write a program, that sorts three numbers. The numbers are taken with `input()`. Example:\n",
"\n",
" First number: 34\n",
" Second number: 12\n",
" Third number: 50\n",
" 50 34 12\n",
" \n",
"#### Hint\n",
"The challenge with this task is, not to miss any of the alternatives. Even with just three numbers there are six different solutions. It is often a challenge, to formalize a situation, which is quite easy in the real world.\n",
"\n",
"The first solution uses nested `if` statement. Whereas the second solution explicitly specifies the six alternatives."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Solution 1\n",
"a = int(input(\"First number: \"))\n",
"b = int(input(\"Second number: \"))\n",
"c = int(input(\"Third number: \"))\n",
"\n",
"if a > b:\n",
" if b > c:\n",
" print(a, b, c)\n",
" else:\n",
" if a > c:\n",
" print(a, c, b)\n",
" else:\n",
" print(c, a, b)\n",
"\n",
"else:\n",
" if b > c:\n",
" if a > c:\n",
" print(b, a, c)\n",
" else:\n",
" print(b, c, a)\n",
" else:\n",
" print(c, b, a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Solution 2\n",
"a = int(input(\"First number: \"))\n",
"b = int(input(\"Second number: \"))\n",
"c = int(input(\"Third number: \"))\n",
"\n",
"if a >= b and b >= c:\n",
" print(a, b, c)\n",
"elif a >= c and c >= b:\n",
" print(a, c, b)\n",
"elif b >= a and a >= c:\n",
" print(b, a, c)\n",
"elif b >= c and c >= a:\n",
" print(b, c, a)\n",
"elif c >= a and a >= b:\n",
" print(c, a, b)\n",
"else:\n",
" print(c, b, a)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}