{ "metadata": { "kernelspec": { "name": "python", "display_name": "Python (Pyodide)", "language": "python" }, "language_info": { "codemirror_mode": { "name": "python", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8" } }, "nbformat_minor": 5, "nbformat": 4, "cells": [ { "id": "6ba892ba-3b99-452b-8bde-bc024199862a", "cell_type": "markdown", "source": "
\n
Y7 - AU2 - L7 More Integers and selection
\n
\n
", "metadata": {} }, { "id": "b61037f1-6db8-4565-97a9-26ca044f9157", "cell_type": "markdown", "source": "
\n
🕐 Do Now: Fix The Code
\n
\n
\n

The code below has several errors that prevent it from functioning properly.

\n

Fix each of the errors and add comments below to explain how you fixed them

\n
\n \n
\n\n
", "metadata": {} }, { "id": "d7bc1608-a7b9-430e-9949-e998087bf31e", "cell_type": "code", "source": "print(\"The following program will ask for two numbers\")\nprint(\"It will then tell you the largest, or if they are equal\")\n\nnum1 = input(\"Enter the first number\")\nnum2 = input(\"Enter the second number\")\n\nif num1 < num2\n print(\"The first number is greater\")\nif num1 > num2\n print(\"The second number is greater\")\nelse:\n print(\"They are equal)\n\n# Space for comments about the changes you made\n# \n ", "metadata": { "trusted": true }, "outputs": [], "execution_count": null }, { "id": "4b29626b-0f58-44a9-a7e9-60b825ebffa4", "cell_type": "markdown", "source": "
\n

Learning Objectives

\n
We are learning how to build a program that utilises selection, casting and integers\n\n
\n
\n
\n\n


\n
⌨️ Coding Activity (A): Build a calculator
\n
\n
\n

Build a program that executes like this

\n\n
\nWelcome to the calculator
\nPlease choose the operation you wish to use:
\n(a) addition (b) subtraction (c) multiplication (d) division
\n
c
\nYou have chosen multiplication
\nEnter number 1
\n
20
\nEnter number 2
\n
2
\n20 X 2 = 40
\n
\n\n
\n\n
\nWelcome to the calculator
\nPlease choose the operation you wish to use:
\n(a) addition (b) subtraction (c) multiplication (d) division
\n
a
\nYou have chosen addition
\nEnter number 1
\n
4
\nEnter number 2
\n
8
\n4 + 8 = 12
\n
\n
\n

The program should run for all four mathematical operations

\n
\n\n
", "metadata": {} }, { "id": "eed231d5-135d-4b7c-9f21-da95e4e3984c", "cell_type": "code", "source": "# Coding Activity A - Build a calculator\n\n\n\n\n\n", "metadata": { "trusted": true }, "outputs": [], "execution_count": 1 }, { "id": "fffc8020-d835-4053-ab6a-943703e31a22", "cell_type": "markdown", "source": "
\n
\n
\n
⌨️ Extension: Additional functions to your calculator
\n \n
\n
\n

Lets make your calculator a bit more useful for higher level maths\n
At the very beginning of your code add the following command

\n
import math

\n

This connects your program to something called a library\n
Libraries allow us to add more functions into python, many of them specialist

\n
\n

Now in the code cell below, add this code

\n
\nnum1 = int(input(\"Enter a number to see its square root\")\nanswer = math.sqrt(num1) \nprint(f\"The square root of {num1} is {answer}\")\n
\n \n
\n

Let's take it further!
\nAdd this library import to top of the code (bef\n

import fractions

\n

Now add this code to the cell below

\n
\nfraction1 = fractions.Fraction(1,2)\nfraction2 = fractions.Fraction(1,3)\n\nanswer = fraction1 * fraction2\n\nprint(f\"{fraction1} X {fraction2} = {answer}\")\n
\n

You can now calculate with fractions!

\n
\n

Add a further option into your calculator program to include square roots and fractions

\n
\n\n
", "metadata": {} }, { "id": "dc9bf77c-65c6-4980-8020-d5c7ae8bfdf7", "cell_type": "code", "source": "\n\n", "metadata": { "trusted": true }, "outputs": [], "execution_count": null } ] }