{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SW03 Class 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": [ "## if elif else \n", "Programming conditions\n", "\n", "Write a short Python program that checks whether the input password is \"1234\" and outputs \"correct\" on the console. If the wrong password was entered, \"wrong\" appears on the console." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "enter your password: 1\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Wrong, you have 2 left\n" ] } ], "source": [ "condition = True\n", "PSW = \"1234\"\n", "\n", "x = input(\"enter your password: \")\n", "att = 0\n", "\n", "while condition and att <= 3:\n", " if x != PSW:\n", " att =+ 1\n", " print(f\"Wrong, you have {3-att} left\")\n", " break\n", " else:\n", " print(\"Correct\")\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculate BMI \n", "\n", "The BMI is a simple measure for classifying body weight. Simply put:\n", "\n", "- <= 18.5 underweight\n", "- <= 25 normal weight\n", "- <= 30 overweight\n", "- \\> 30 Obesity\n", "\n", "Write the code that calculates the BMI from the following variables, outputs it and the classification. E.g. ***BMI 24.4 - normal weight***\n", "\n", "```python\n", "weight = 79\n", "height = 1.80\n", "```\n", "\n" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your weight: 80\n", "Enter your high: 1.80\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "BMI 24.0 - normal weight\n" ] } ], "source": [ "w = float(input(\"Enter your weight: \"))\n", "h = float(input(\"Enter your high: \"))\n", "\n", "if h > 3:\n", " h = h/100\n", "else:\n", " h\n", "\n", "BMI = int(w/(h**2))\n", "\n", "while True:\n", " if BMI <= 18.5:\n", " print(f\"BMI {BMI:.1f} - underweith\")\n", " break\n", " \n", " elif 18.5 < BMI <= 25:\n", " print(f\"BMI {BMI:.1f} - normal weight\")\n", " break\n", " \n", " elif 25 < BMI == 30:\n", " print(f\"BMI {BMI:.1f} - overweight\")\n", " break\n", " \n", " else:\n", " print(f\"BMI {BMI:.1f} - obesity\")\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sum up numbers \n", "\n", "Write a program that reads 5 numbers, sums them up and finally outputs the sum of the integers on the console\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Write the 1st number: 5\n", "Write the 2nd number: 4\n", "Write the 3rd number: 7\n", "Write the 4th number: 5\n", "Write the 5th number: 7\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "28\n" ] } ], "source": [ "# With 5 variables --> D.R.Y.\n", "\n", "n1 = float(input(\"Write the 1st number:\" ))\n", "n2 = float(input(\"Write the 2nd number:\" ))\n", "n3 = float(input(\"Write the 3rd number:\" ))\n", "n4 = float(input(\"Write the 4th number:\" ))\n", "n5 = float(input(\"Write the 5th number:\" ))\n", "\n", "while True:\n", " a = (n1,n2,n3,n4,n5)\n", " print(int(sum(a)))\n", " break" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Write a number: 864\n", "Write a number: 87654\n", "Write a number: 765\n", "Write a number: 765\n", "Write a number: 876\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "The sum is 90924.0\n" ] } ], "source": [ "# With 1 variable\n", "\n", "n1 = 0\n", "counter = 0\n", "\n", "while True:\n", " counter += 1\n", " n1 += float(input(\"Write a number:\" ))\n", "\n", " if counter == 5:\n", " break\n", "\n", "print(f\"The sum is {n1}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Fizzbuzz \n", "\n", "Program FizzBuzz:\n", "\n", "We want to write a program that counts from **0 to 100** and outputs these numbers. However, we want:\n", "- Whenever a number is **divisible by 3**, print **Fizz** instead\n", "- Whenever a number is **divisible by 5**, print **Buzz** instead.\n", "- Whenever a number is **divisible by 3 and 5**, print **FizzBuzz**." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "Fizz\n", "4\n", "Buzz\n", "Fizz\n", "7\n", "8\n", "Fizz\n", "Buzz\n", "11\n", "Fizz\n", "13\n", "14\n", "FizzBuz\n", "16\n", "17\n", "Fizz\n", "19\n", "Buzz\n", "Fizz\n", "22\n", "23\n", "Fizz\n", "Buzz\n", "26\n", "Fizz\n", "28\n", "29\n", "FizzBuz\n", "31\n", "32\n", "Fizz\n", "34\n", "Buzz\n", "Fizz\n", "37\n", "38\n", "Fizz\n", "Buzz\n", "41\n", "Fizz\n", "43\n", "44\n", "FizzBuz\n", "46\n", "47\n", "Fizz\n", "49\n", "Buzz\n", "Fizz\n", "52\n", "53\n", "Fizz\n", "Buzz\n", "56\n", "Fizz\n", "58\n", "59\n", "FizzBuz\n", "61\n", "62\n", "Fizz\n", "64\n", "Buzz\n", "Fizz\n", "67\n", "68\n", "Fizz\n", "Buzz\n", "71\n", "Fizz\n", "73\n", "74\n", "FizzBuz\n", "76\n", "77\n", "Fizz\n", "79\n", "Buzz\n", "Fizz\n", "82\n", "83\n", "Fizz\n", "Buzz\n", "86\n", "Fizz\n", "88\n", "89\n", "FizzBuz\n", "91\n", "92\n", "Fizz\n", "94\n", "Buzz\n", "Fizz\n", "97\n", "98\n", "Fizz\n", "Buzz\n" ] } ], "source": [ "x = 0\n", "while x < 100:\n", " x += 1\n", "\n", " if x % 3 == 0 and x % 5 == 0:\n", " print(\"FizzBuz\")\n", "\n", " elif x % 3 == 0:\n", " print(\"Fizz\")\n", "\n", " elif x % 5 == 0:\n", " print(\"Buzz\")\n", "\n", " else:\n", " print(x)" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 4 }