LUNAR
π
Dashboard
Browse
SW06_InClass_EN
JUPYTER
View Source
{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SW06 InClass - Repetition and Programming \n", "\n", "This file is intended for interactive revision in the first hour of class. You write directly in the lines of code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following functions should be programmed together in the class:\n", "- dog-year\n", "- bmi\n", "- fahrenheit_celsius converter\n", "- Fibonacci number\n", "- Fibonacci list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## dog-year function \n", "\n", "Function that converts the age of a dog into human years" ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "35\n" ] } ], "source": [ "def dog(human:int)->int:\n", " return human * 7\n", "\n", "print(dog(5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## BMI function \n", "\n", "Define the function bmi(...), which has the two parameters height[kg] and weight[m] and returns the BMI to two decimal places. Then call the function with your personal values ββor, even better, write a user input with input()." ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Your height: 1.90m \n", "Your weight: 80kg \n", "Your BMI: 22.16\n" ] } ], "source": [ "usr_height = input(\"Height in m: \")\n", "usr_weight = input(\"Weight in kg: \")\n", "\n", "def bmi(height:float, weight:float) -> float:\n", " \"\"\"\n", " Calculate BMI\n", "\n", " Parameters:\n", " height (float): height of a person in meters\n", " weight (float): weight of a person in kilos\n", " Return:\n", " bmi (float): BMI of the person\n", " \"\"\"\n", "\n", " bmi_value = weight / (height**2)\n", " return bmi_value\n", "\n", "print(f\"Your height: {usr_height}m \\nYour weight: {usr_weight}kg \\nYour BMI: {round(bmi(float(usr_height),float(usr_weight)),2)}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Fahrenheit-Celsius converter \n", "\n", "Conversion according to the formula Β°C = (Β°F-32)/1.8" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "37.78\n" ] } ], "source": [ "usr = input(\"Degree in Fahrenheir: \")\n", "def celsius(f_deg:float)->float:\n", " c_deg = (f_deg - 32) / (9/5)\n", " return c_deg\n", "\n", "print(round(celsius(int(usr)),2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Fibonacci number \n", "\n", "Print the nth Fibonacci number with a while loop and with a for loop" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "1\n", "2\n", "3\n", "5\n", "8\n", "13\n", "21\n", "34\n" ] } ], "source": [ "def fibonacci1(n):\n", " x = 0\n", " y = 1\n", " print(x)\n", " print(y)\n", " count = 0\n", " while count < n-2:\n", "\n", " sum = x + y\n", " x = y\n", " y = sum\n", " print(sum)\n", "\n", " count += 1\n", "\n", "fibonacci1(10)" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "1\n", "2\n", "3\n", "5\n", "8\n", "13\n", "21\n", "34\n" ] } ], "source": [ "def fibonacci(n):\n", " x = 0\n", " y = 1\n", " print(x)\n", " print(y)\n", " \n", " for _ in range(n-2):\n", "\n", " sum = x + y\n", " x = y\n", " y = sum\n", " print(sum)\n", "\n", "fibonacci(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create Fibonacci number list \n", "\n", "Function which creates a list of Fibonacci numbers up to m - use function above" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def my_list(m):\n", " x = 0\n", " y = 1\n", " fib_list = [x,y]\n", "\n", " for _ in range(m-2):\n", " sum = x + y\n", " fib_list.append(sum)\n", " x = y\n", " y = sum\n", " return fib_list\n", "\n", "my_list(10)" ] } ], "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.12.7" } }, "nbformat": 4, "nbformat_minor": 2 }