You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2057 lines
54 KiB
Plaintext

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "EsfO5Q92tL-6"
},
"source": [
"[![freeCodeCamp](https://cdn.freecodecamp.org/testable-projects-fcc/images/fcc_secondary.svg)](https://freecodecamp.org/)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "OikF86Tfxsrq"
},
"source": [
"**Learn Foundational Math 1 by Building an Equation Solver**<br>\n",
"Each of these steps will lead you toward the Certification Project. First you have to copy the files and set them up in your Google Drive."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "szp5flp1fA8-"
},
"source": [
"# &darr; **Do this first** &darr;\n",
"Make sure you are logged into your Google account, and copy this notebook to your own account. Click \"File\" (at the top of this page) and then click \"Save a copy in Drive.\" The file will be in a folder called \"Colab Notebooks\" in your Google Drive."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jVXoY1mmfBjG"
},
"source": [
"#Directions - Click to expand the next step<br>\n",
"Click on the little <i>triangle</i> next to the word \"Step\" to do that step. Once you complete a step, click the triangle to expand the next step."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FnqxVtNYIA8D"
},
"source": [
"# Step 0 - Acquire the testing library"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jGDR7mBeh-ov"
},
"source": [
"Please run this code to get the library file from FreeCodeCamp. Each step will use this library to test your code. You do not need to edit anything; just run this code cell and wait a few seconds until it tells you to go on to the next step."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 373
},
"id": "V88D7Zrei2ym",
"outputId": "5c8cbb14-a430-46e0-cf84-ca8c2566daa8",
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: requests in /usr/local/lib/python3.8/dist-packages (2.28.2)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.8/dist-packages (from requests) (3.1.0)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from requests) (2019.11.28)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/lib/python3/dist-packages (from requests) (2.8)\n",
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/lib/python3/dist-packages (from requests) (1.25.8)\n",
"\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
"\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.0.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.1.2\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3 -m pip install --upgrade pip\u001b[0m\n",
"\n",
" Test passed. You can go on to the next step.\n"
]
}
],
"source": [
"# You may need to run this cell at the beginning of each new session\n",
"\n",
"!pip install requests\n",
"\n",
"# This will just take a few seconds\n",
"\n",
"import requests\n",
"\n",
"# Get the library from GitHub\n",
"url = 'https://raw.githubusercontent.com/freeCodeCamp/cdn/main/build/math-cert-tests/math-code-test-a.py'\n",
"r = requests.get(url)\n",
"\n",
"# Save the library in a local working directory\n",
"with open('math_code_test_a.py', 'w') as f:\n",
" f.write(r.text)\n",
"\n",
"# Now you can import the library\n",
"import math_code_test_a as test\n",
"\n",
"# This will tell you if the code works\n",
"test.step19()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EjxsoAEij2z5"
},
"source": [
"# Step 1 - Add"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tYdScbUgkif-"
},
"source": [
"To help you get familiar with Colab notebooks, you will start with the basics. Python uses `+`, `-`, `*`, and `/` for the four math operations: <i>add</i>, <i>subtract</i>, <i>multiply</i>, and <i>divide</i>. When you add two numbers, the result is the <i>sum</i>. Use addition within the `print` statement to get the sum of `a` and `b`. To run the code, you can hit \"shift\" and \"enter\" or you can click the run button (the triangle inside a circle)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "muuUL56MknVm",
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"a=1\n",
"b=2\n",
"\n",
"# Change the next line to print the sum of a and b\n",
"print(a+b)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step01(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yAR4qoEcj7tv"
},
"source": [
"# Step 2 - Subtract"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kxpYn-rfjomx"
},
"source": [
"When you subtract two numbers, the result is the <i>difference</i>. Use subtraction in the print statement to get the difference between `c` and `d`. Remember to use \"shift\" and \"enter\" to run the code.\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "cLscGSGcjtPM",
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"c = 7\n",
"d = 3\n",
"\n",
"# Change the next line to print the positive difference between c and d\n",
"print(c-d)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step02(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9n3Rf_u4kVRc"
},
"source": [
"# Step 3 - Multiply"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "upss2rbrkNE3"
},
"source": [
"When you multiply numbers, the result is the <i>product</i>. Use multiplication within the print statement to get the product of `e` and `f`:\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "riH3m4YekQNk",
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"e = 2\n",
"f = 4\n",
"\n",
"# Change the next line to print the product of e and f\n",
"print(e*f)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step03(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "33FosVOV1NBu"
},
"source": [
"# Step 4 - Divide\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "odHJk50S1V05"
},
"source": [
"When you divide two numbers, the result is the <i>quotient</i>. Use division within the print statement to get the quotient of `g` and `h`:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "5YWS8tuA1UMc",
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.0\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"g = 8\n",
"h = 4\n",
"\n",
"# Change the next line\n",
"print(g/h)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step04(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rujSYyXZ3AOY"
},
"source": [
"# Step 5 - Cast Input"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ttrSrlGh3CK8"
},
"source": [
"User input comes in as a <i>string</i>, so you need to cast it as an <i>integer</i> or a <i>float</i> before doing any math. The code below asks for input and uses `int()` to cast it as an integer. Follow the model and cast the second variable as an integer. Then run the code an test it. (Remember to hit \"enter\" after you type each integer in the box.)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "fWYQrvOP3Cif",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a positive integer: 4\n",
"Enter another positive integer: 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"9\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"strA = input('Enter a positive integer: ')\n",
"intA = int(strA)\n",
"\n",
"strB = input('Enter another positive integer: ')\n",
"\n",
"# Change the next line but keep the variable name:\n",
"intB = int(strB)\n",
"\n",
"\n",
"print(intA+intB)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step05(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_NVz--_vqAxA"
},
"source": [
"#Step 6 - Input and cast on the same line"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "M7tIwnUdqUBp"
},
"source": [
"You can prompt for input and cast that input on the same line. Notice the nested functions in the first line of code. Follow that model to prompt for input and cast that input as an integer on the same line."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"id": "I9a4sf1aqYZd",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter an integer: 5\n",
"Enter an integer: 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"7\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"intA = int(input('Enter an integer: '))\n",
"\n",
"# Change the next line but keep the variable name:\n",
"intB = int(input('Enter an integer: '))\n",
"\n",
"\n",
"print(intA+intB)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step06(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-lFO35p7l3cL"
},
"source": [
"#Step 7 - Float numbers"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Qb0X7hCIl5yO"
},
"source": [
"A <i>float</i> number allows decimal places. When prompting for a number as input, casting that as a <i>float</i> is usually the best choice. Follow the model below to prompt for input and cast that input as a <i>float</i> on the same line."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"id": "tKFaQlQ4l8ti",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a number: 43.53\n",
"Enter a number: 342.543\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.1270789360751789\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"a = float(input('Enter a number: '))\n",
"\n",
"# Change the next line but keep the variable name:\n",
"b = float(input('Enter a number: '))\n",
"\n",
"\n",
"print(a/b)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step07(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6PeVH4Gx3PE3"
},
"source": [
"# Step 8 - Order of Operations"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tComwmxt3UuP"
},
"source": [
"You may have heard of the <b>order of operations</b> and the acronym <b>PEMDAS</b>, which reminds you of the correct order. This means that you do what is in <b>Parentheses</b> first, then simplify <b>Exponents</b>. You then do all of the <b>Multiplication</b> and <b>Division</b> together, as long as you work from left to right and simplify them in order. The same is true of <b>Addition</b> and <b>Subtraction</b>: work from left to right and simplify the one the comes up next. Python knows the order of operations. In the following code, Python will calculate the actual_answer correctly. Notice the use of `**` to indicate an exponent. Do the arithmetic in your head (no writing code) and change the `your_answer` variable, then run the code to see if your_answer matches the actual_answer."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"id": "d-3CdCL53VHA",
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Actual answer is 8.0\n",
"Your answer is 8\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"actual_answer = (1+4*2-14/2)**3\n",
"\n",
"# Put your answer on the following line:\n",
"your_answer = 8\n",
"\n",
"print('Actual answer is ', actual_answer)\n",
"print('Your answer is ', your_answer)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step08(your_answer)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "D5Z8_9gN39TD"
},
"source": [
"# Step 9 - Remainder and Modulus"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IBEwjqML39TH"
},
"source": [
"A <i>remainder</i> is what is left over when you try to divide two numbers and it doesnt divide evenly. The remainder of 10 / 4 is 2 because 4 goes into 10 two whole times, with 2 left over. The <i>modulus</i> (`%`) operator will output the remainder, so `10 % 4` will return 2. Use the modulus operator to find the remainder of a divided by b:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"id": "EonWJtHS39TI",
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"a = 14\n",
"b = 6\n",
"\n",
"# Change this line\n",
"print(a%b)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step09(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rPT2OymY4HdE"
},
"source": [
"# Step 10 - Modulus and Factors"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ct7JeTNm4HdF"
},
"source": [
"Use an `if` statement with the modulus operator to find out if one number is a factor of another. For example, to see if 5 is a factor of 20, you can test `if 20 % 5 == 0`. If there's no remainder, the second number is a factor of the first. Remember that Python comparisons use `==` to test values. Remember that the `if` statement ends in a colon (`:`) and the resulting block is indented four spaces. Finish the code below to print \"true\" if `test_factor` is a factor of `number` and print \"false\" if it is not."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"id": "fRe04K3a4HdF",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter an integer: 14\n",
"Enter an integer to see if its a factor: 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"true\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"number = int(input('Enter an integer: '))\n",
"test_factor = int(input('Enter an integer to see if its a factor: '))\n",
"\n",
"# Change the next line to test the factor:\n",
"if number % test_factor == 0:\n",
" print('true')\n",
"else:\n",
" print('false')\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step10(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yED2Dox44JYj"
},
"source": [
"# Step 11 - Finding Factors"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "oMQEeGVI4JYk"
},
"source": [
"Now you will find all of the factors of a number. This code has a loop with a variable, `test_factor`, that iterates through a defined range. Remember that the first line defining the loop ends in a colon (:) and each line in the loop requires a four-space indent. Change the `if` statement to find all the factors of `number`."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"id": "YQqur5gF4JYk",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter an integer: 24\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"6\n",
"8\n",
"12\n",
"24\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"number = int(input('Enter an integer: '))\n",
"\n",
"# Only change the if statement:\n",
"for test_factor in range(1, number+1):\n",
" if number % test_factor == 0:\n",
" print(test_factor)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step11(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uQdfUUlw4Kfg"
},
"source": [
"# Step 12 - Prime Numbers"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ngn_lXfv4Kfh"
},
"source": [
"A <i>prime</i> number is a number whose only factors are 1 and itself. The number 5 is prime because its only factors are 1 and 5, but the 6 is not prime because it has 1, 2, 3, and 6 as factors. Any number that is not a prime is a <i>composite</i>. For each iteration in the loop, `test_number` will be a possible factor. Change the `if` statement so that the code prints \"composite\" if `number` is not prime."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"id": "9q6wtSTg4Kfh",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a positive integer: 25\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"composite\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"number = int(input(\"Enter a positive integer: \"))\n",
"\n",
"prime_or_comp = \"prime\"\n",
"\n",
"for test_number in range(2,number):\n",
" # Change the if statement to test one factor here:\n",
" if number % test_number == 0:\n",
" prime_or_comp = \"composite\"\n",
" break\n",
"\n",
"print(prime_or_comp)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step12(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "XEf8vCUH4Lc_"
},
"source": [
"# Step 13 - Reciprocals"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QrV7DGum4Lc_"
},
"source": [
"A *reciprocal* is a number \"flipped.\" The reciprocal of $\\frac{2}{3}$ is $\\frac{3}{2}$ and the reciprocal of 5 is $\\frac{1}{5}$ because whole numbers have denominators of 1. You can multiply a number by its reciprocal to get 1, so 5 * $\\frac{1}{5}$ = 1 and $\\frac{2}{3}$ * $\\frac{3}{2}$ = 1. To get the reciprocal of a number, take 1 divided by that number. Trying to get the reciprocal of zero will lead to a \"divide by zero\" error. Use a print statement to output the reciprocal of `n` as a decimal."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"id": "rwExvDuM4LdA",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a number: 25\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.04\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"n = float(input('Enter a number: '))\n",
"\n",
"# Write your code here\n",
"print(1/n)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step13(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EyV2Udzs4MVS"
},
"source": [
"# Step 14 - Splitting input"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_GoKZbiS4MVS"
},
"source": [
"The code below asks for two integers, separated by a comma, then splits the input at the comma. Notice the input remains a string, then the `split()` function creates an array with two elements. Finish the following code to cast the two variables `a` and `b` as `float` numbers, then divide the two numbers and print the result."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"id": "axr86XQ04MVS",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter two numbers, separated by a comma: 21,56\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.375\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"nums = input('Enter two numbers, separated by a comma: ')\n",
"sp = nums.split(\",\")\n",
"\n",
"# Use the next line as a model:\n",
"a = float(sp[0])\n",
"\n",
"# Change the next line to cast the number as a float:\n",
"b = float(sp[1])\n",
"\n",
"# Change the print statement:\n",
"print(a/b)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step14(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "76XeyVVM4NrX"
},
"source": [
"# Step 15 - Square Numbers"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cNCxDnCt4NrX"
},
"source": [
"One factor multiplied by itself will produce a <i>square</i> number, so a number raised to an exponent of 2 is that number <i>squared</i> (like calculating the area of a square). Python uses `**` to indicate exponents. Complete the code to print the square of the input."
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"id": "ZyikhBYd4NrX",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a number to square: 21\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"441.0\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"n = float(input('Enter a number to square: '))\n",
"\n",
"# Change this line of code:\n",
"print(n**2)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step15(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "B4WZJ0Iv4O1d"
},
"source": [
"# Step 16 - Square Root Function"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "R3RBO7tF4O1d"
},
"source": [
"You can find the square root of a number with the `sqrt()` function. To use this function, you need to import the math library. This library enables you to use many functions, as you will see in later steps. To get the square root of x, you would write `math.sqrt(x)`. Complete the code to print the square root of a number."
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"id": "MSkW_Md24O1d",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a number to find the square root: 23\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.795831523312719\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"import math\n",
"\n",
"n = float(input('Enter a number to find the square root: '))\n",
"\n",
"# Change the next line of code:\n",
"print(math.sqrt(n))\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step16(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1hru9M7J4P48"
},
"source": [
"# Step 17 - Floor Function"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VG-12xT94P48"
},
"source": [
"The`floor()` function drops any decimals and sometimes is called the <i>integer part</i> of a number. Complete the code to print the floor of a number. Notice you `import math` and use `math.floor(n)` "
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"id": "F-c0gsQL4P48",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a number with decimal places: 32.3425243\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"32\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"import math\n",
"\n",
"n = float(input('Enter a number with decimal places: '))\n",
"\n",
"# Change the next line of code:\n",
"print(math.floor(n))\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step17(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cG_JMTeb4Q-E"
},
"source": [
"# Step 18 - Finding Square Factors"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "oN6isUiz4Q-E"
},
"source": [
"This step will combine a few things you have already done. Remember that a square number is an integer that is the result of multiplying another integer by itself. Just as you created a loop to find factors of an integer, here you will find the greatest factor that is a perfect square. For example, 2 is a factor of 16, but 2 is not a square number, while 4 is a factor and it is a square number, but it is not the greatest square factor. The greatest square factor of 16 is 16. The greatest square factor of 32 is 16. Complete `if` statement in the loop to find the greatest square factor of a number."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"id": "SRbIgfWr4Q-E",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter an integer to find the greatest square factor: 34434\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"9\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"import math\n",
"\n",
"n = int(input('Enter an integer to find the greatest square factor: '))\n",
"\n",
"max_factor = 1\n",
"upper_limit = math.floor(math.sqrt(n)) + 1\n",
"\n",
"# Change one line in this loop:\n",
"for maybe_factor in range(1,upper_limit):\n",
" if n % (maybe_factor**2) == 0:\n",
" max_factor = maybe_factor\n",
"\n",
"# Keep this print statement:\n",
"print(max_factor**2)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step18(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "a6Kr7wKq4SBP"
},
"source": [
"# Step 19 - Dividing out Factors"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dlB9GF164SBP"
},
"source": [
"Building upon your code from the previous step, this code will divide out the greatest square factor of a number. You don't need to change anything; just run the code below a few times, inputting different numbers each time."
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"id": "0KCr-ImL4SBQ",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter an integer to factor: 4336\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" 4336 = 16 * 271.0\n",
"\n",
" Test passed. You can go on to the next step.\n"
]
}
],
"source": [
"import math\n",
"\n",
"n = int(input('Enter an integer to factor: '))\n",
"upper_limit = math.floor(math.sqrt(n)) + 1\n",
"square_root = 1\n",
"max_factor = 1\n",
"other_factor = 1\n",
"\n",
"# Notice what the loop is doing here\n",
"for maybe_factor in range(1, upper_limit):\n",
" # Check for square factors\n",
" if n % (maybe_factor**2) == 0:\n",
" # Find the greatest square factor\n",
" max_factor = maybe_factor**2\n",
"\n",
"# Divide out the greatest square factor\n",
"other_factor = n/max_factor\n",
"\n",
"# Display the results\n",
"print(\"\", n, \" = \", max_factor, \" * \", other_factor)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step19()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IIb5MHzI4TAU"
},
"source": [
"# Step 20 - Factoring Square Roots"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "F9mLn2vR4TAU"
},
"source": [
"The last four steps prepared you for this. To factor a square root, you want to divide out any perfect square factors. For example:<br>\n",
" $\\sqrt{12}$ = $\\sqrt{4 * 3}$ = 2$\\sqrt{3}$<br>\n",
"Because 4 is a square number, the square root of 4 is now outside the radical.\n",
"You will import `sympy` and `symbols` to use the radical ($\\sqrt{x}$) in the output. Use the code from the previous step (without changing much). Your goal is to ask for a number and output the factored square root. The radical formatting (using sympy and symbols) is already done for you."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"id": "ucg7J-8m4TAU",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Without the radical, enter a square root to factor: 32\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" Test passed. This is this your factored square root:\n",
"\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle 4 \\sqrt{2}$"
],
"text/plain": [
"4*sqrt(2)"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import math\n",
"import sympy\n",
"from sympy import symbols\n",
"\n",
"n = int(input('Without the radical, enter a square root to factor: '))\n",
"\n",
"# Use these variables\n",
"upper_limit = math.floor(math.sqrt(n)) + 1\n",
"max_factor = 1\n",
"other_factor = 1\n",
"square_root = 1\n",
"\n",
"# Notice what the loop is doing here\n",
"for maybe_factor in range(1, upper_limit):\n",
" if n % (maybe_factor**2) == 0:\n",
" max_factor = maybe_factor**2\n",
"\n",
"# Divide out the greatest square factor\n",
"other_factor = n/max_factor\n",
"\n",
"# Output - keep this:\n",
"square_root = int(math.sqrt(max_factor))\n",
"other_factor = int(other_factor)\n",
"output = square_root*sympy.sqrt(other_factor)\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step20()\n",
"output"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4VnLdwgZ4WHz"
},
"source": [
"# Step 21 - Rounding"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3fxJA-yI4WH0"
},
"source": [
"If you only want a certain number of decimal places, use the `round()` function. This takes two arguments: the number to round and the number of decimal places, so `round(2.468, 2)` will return `2.47`. To round a large number instead of a decimal number, make the second argument negative, so `round(2345, -3)` will return `2000`. Finish the code below so that it prints the first number rounded to the nearest million (six zeros) and the second number rounded to 3 decimal places."
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"id": "4n4mYI554WH0",
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15000000\n",
"0.007\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"a = 14588132\n",
"b = 0.006538298336\n",
"\n",
"# Write your code here\n",
"print(round(a, -6))\n",
"print(round(b, 3))\n",
"\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step21(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cVC_ENRHfIP4"
},
"source": [
"# Step 22 - Fractions, Decimals, Percents"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8cZnGQO4fWzS"
},
"source": [
"To convert a decimal number to a fraction, let $x$ = the number of decimal places. The basic fraction is the number (without the decimal point) over 10$^{x}$. Example: 0.2 = $\\frac{2}{10}$ and 0.34 = $\\frac{34}{100}$ and 0.567 = $\\frac{567}{1000}$. In some cases, you may be able to reduce that fraction. Because \"percent\" means \"out of 100\" the percent refers to the first two decimal places. Complete the code to ask for a decimal input, then print the fraction and the percent. Hint: The `exponent` variable gives you the number of decimal places."
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"id": "HPyBoXwNfhWQ",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a decimal number to convert: .2342341\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The decimal is 0.2342341\n",
"The fraction is 2342341 / 10000000\n",
"The percent is 23.42341 %\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"import math\n",
"\n",
"digits = input(\"Enter a decimal number to convert: \")\n",
"exponent = int(len(digits))-1\n",
"n = float(digits)\n",
"\n",
"# Change the values of these three variables\n",
"numerator = int(n*10**exponent)\n",
"denominator = 10**exponent\n",
"percent = (numerator/denominator)*100\n",
"\n",
"# Output - keep this\n",
"print(\"The decimal is \", n)\n",
"print(\"The fraction is \", numerator, \"/\", denominator)\n",
"print(\"The percent is \", percent, \" %\")\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step22(n,numerator,denominator,percent,exponent)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Llx2Kaau8Dsz"
},
"source": [
"# Step 23 - Defining a Function"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "PUjA2-X_8OER"
},
"source": [
"To execute a block of code with one command, define a <i>function</i> with the `def` command and the name of the function. Notice everything in the function is indented 4 spaces. Run the following code to see an example. Then change the function name to `fun()` and also change the name where you call the function. Run the code again."
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"id": "5xeG1__m8h1P",
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is outside the function\n",
"This is in the function\n",
"Back outside the function\n",
" \n",
"Code test passed\n",
"Go on to the next step\n"
]
}
],
"source": [
"# Define a function\n",
"def fun():\n",
" print(\"This is in the function\")\n",
"\n",
"# Other code not in the function\n",
"print(\"This is outside the function\")\n",
"\n",
"# Call the function\n",
"fun()\n",
"\n",
"print(\"Back outside the function\")\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step23(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tMa9hDVL85sl"
},
"source": [
"# Step 24 - Function with Input"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EHxRLGSz9JqT"
},
"source": [
"A <i>function</i> can take input (called an \"argument\") and do something with that input. Use `def` to define the function, and include a variable in the parentheses to represent the argument. Indent everything that is a part of the function. When calling the function, pass the argument to it in the parentheses. Run the following code to see this example. Then change the `input()` variable name to `nombre` and also change that variable name in the argument when you call the function. Run the code again."
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"id": "RDI_QfDQ9R_q",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"What is your name? \n",
" Python\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello Python\n",
" \n",
"Code test passed\n",
"You can go on to the next step\n"
]
}
],
"source": [
"# Define a function\n",
"def greeting(name):\n",
" print(\"Hello \", name)\n",
"\n",
"nombre = input(\"What is your name? \\n\")\n",
"\n",
"# Call the function\n",
"greeting(nombre)\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step24(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "d1WVETzF9-Dv"
},
"source": [
"# Step 25 - Function with Two Inputs"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VYhQLZsV-FSr"
},
"source": [
"To pass more than one argument to a function, separate the arguments with commas. Run the code to see the example, then add a third argument to the function and run it again. The `third` variable is already in the code. Change three lines of code to use that variable: (1) the argument when you call the function, (2) the argument when you define the function, (3) the `sum` line within in the function definition."
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"id": "0OGuCCOz_VsV",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a number: \n",
" 231\n",
"Enter another number: \n",
" 321\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The sum is 555.0\n",
" \n",
"Code test passed\n",
"You can go on to the next step\n"
]
}
],
"source": [
"# Define function\n",
"def add(a,b, c):\n",
" # Use c for the third variable\n",
" sum = a+b+c\n",
" print(\"The sum is \", sum)\n",
"\n",
"first = float(input(\"Enter a number: \\n\"))\n",
"second= float(input(\"Enter another number: \\n\"))\n",
"third = 3\n",
"\n",
"# Call the function\n",
"add(first,second, third)\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step25(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xmlkOkEc-iqL"
},
"source": [
"# Step 26 - Function with Return Value"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "KdLW1Msd-pPg"
},
"source": [
"Instead of including a `print()` statement within the function, the function can `return` a value right where you call it. To make the function return a value, use the `return` statement. Run the following code to see an example, then change the `return` statement to multiply by 3 instead of 2 and run the code again."
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"id": "4JELM_kc-zMP",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a number: \n",
" 322\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your number multiplied = 966.0\n",
" \n",
"Code test passed\n",
"You can go on to the next step\n"
]
}
],
"source": [
"# define the function\n",
"def multiplied(number):\n",
" return number*3\n",
"\n",
"a = float(input(\"Enter a number: \\n\"))\n",
"print(\"Your number multiplied = \", multiplied(a))\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step26(In[-1].split('# Only change code above this line')[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "98oHO4mh4X3e"
},
"source": [
"# Step 27 - Solving for x"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jYh9MRv24X3e"
},
"source": [
"In Algebra, `X` often refers to the unknown number in an equation. To find the value of `x` we use algebra rules to get to `x = ` [some number]. SymPy is a Python library to work with symbolic math. The following code works to solve an equation set equal to zero. Run the code and remember to use Python syntax to enter an equation (with \"x\" as the variable) and see the solution."
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"id": "H4HFJdc34X3e",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter an equation to solve for x: 0 = 23*x**2 + 32*x + 2342\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"x = -16/23 - sqrt(53610)*I/23\n",
"If you didn't get a syntax error, you are ready for the project\n"
]
}
],
"source": [
"import sympy\n",
"from sympy import symbols\n",
"from sympy.solvers import solve\n",
"\n",
"x = symbols('x')\n",
"\n",
"eq = input('Enter an equation to solve for x: 0 = ')\n",
"print(len(solve(eq,x)))\n",
"print(\"x = \", solve(eq,x)[0])\n",
"\n",
"\n",
"# Only change code above this line\n",
"import math_code_test_a as test\n",
"test.step27(In[-1].split('# Only change code above this line')[0])\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xtI0kyR3RpFw"
},
"source": [
"# Step 28 - Make your own Functions"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "j9jLJyL-Q5T9"
},
"source": [
"Building upon what you did in previous steps, define a different function for each of the following:<br>\n",
"<ul>\n",
" <li>Add, subtract, multiply, divide</li>\n",
" <li>Detect prime numbers</li>\n",
" <li>Generate prime factors of a number</li>\n",
" <li>Simplify square roots</li>\n",
" <li>Solve for a variable</li>\n",
"</ul>\n",
"Each function should prompt the user with a question, take input, and output the answer."
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {
"id": "wmtRDDigPrXZ",
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Input a radical/square root number (without symbol) to find it's simplified form: 64\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle 8$"
],
"text/plain": [
"8"
]
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code here\n",
"import re\n",
"import sympy\n",
"from sympy import symbols\n",
"from sympy.solvers import solve\n",
"\n",
"def arithematic():\n",
" print(\"Enter an equation comprising only of numbers seprated by aithematic operators:\")\n",
" eq = input()\n",
" if (re.search(\"^(\\s?[0-9]+\\s?[+-\\\\\\*]\\s?)+[0-9]+$\", eq)):\n",
" result = eval(eq)\n",
" print(f'{eq} = {result}')\n",
" else:\n",
" print(\"Error: equation is not in accepted format\")\n",
"\n",
"def get_smallest_prime_factor(num):\n",
" if (num % 2 == 0 and num != 2):\n",
" return 2\n",
" \n",
" for i in range(3, int(num/2)+1, 2):\n",
" if num % i == 0:\n",
" return i\n",
"\n",
" return 1\n",
" \n",
"def is_prime():\n",
" try:\n",
" num = int(input(\"Type a natural number to test if it a prime number:\"))\n",
" except Exception as e:\n",
" print(\"Error:\", e)\n",
" return\n",
" \n",
" smallest_prime_factor = get_smallest_prime_factor(num)\n",
" if (smallest_prime_factor == 1):\n",
" print(f\"{num} is a prime number.\")\n",
" else:\n",
" print(f\"{num} is not a prime number and is divisible by {smallest_prime_factor}\")\n",
"\n",
"def prime_factorization():\n",
" try:\n",
" num = int(input(\"Type a natural number to find it's prime factors:\"))\n",
" except Exception as e:\n",
" print(\"Error:\", e)\n",
" \n",
" factors = []\n",
" new_num = num\n",
" while True:\n",
" factor = get_smallest_prime_factor(new_num)\n",
" if (factor == 1):\n",
" factors.append(int(new_num))\n",
" break\n",
" else:\n",
" factors.append(factor)\n",
" new_num /= factor\n",
" print(f\"Prime factors of {num} are:\")\n",
" print(' x '.join([str(n) for n in factors]))\n",
"\n",
"def simplify_sqrt():\n",
" try:\n",
" sqrt = int(input(\"Input a radical/square root number (without symbol) to find it's simplified form:\"))\n",
" except Exception as e:\n",
" print(\"Error:\", e)\n",
" return\n",
" \n",
" maybe_factor = 2\n",
" max_factor = 1\n",
" while (maybe_factor**2 <= sqrt):\n",
" if (sqrt % maybe_factor**2 == 0):\n",
" max_factor = maybe_factor\n",
" maybe_factor += 1\n",
" return max_factor*sympy.sqrt(int(sqrt/max_factor**2))\n",
"\n",
"def solve_for_x():\n",
" try:\n",
" eq = input(\"Type an equation to solve for x. 0 = \")\n",
" x = symbols(\"x\")\n",
" solutions = solve(eq, x)\n",
" for s in solutions:\n",
" print(\"x =\", s)\n",
" except BaseException as e:\n",
" print(f\"Error: {e}\")\n",
"\n",
"# This step does not have test"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lgqRbYTWTIst"
},
"source": [
"# Step 29 - Create a Menu"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "12Ki56SNR4te"
},
"source": [
"Use print statements to create a menu that displays a numbered list of options. Then prompt for user input to choose an option. Use an `if` statement to print a different message for each option in the menu."
]
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {
"id": "bEy0V41ASSjX",
"tags": []
},
"outputs": [],
"source": [
"# Write your code here\n",
"from IPython.display import display\n",
"\n",
"options = [(\"Simple calculations\", arithematic),\n",
" (\"Check if a number is prime\", is_prime),\n",
" (\"Prime factorize a number\", prime_factorization),\n",
" (\"Find simplified form of a square root\", simplify_sqrt),\n",
" (\"Solve an euqation for X\", solve_for_x)]\n",
"\n",
"def print_menu():\n",
" print(\"What would you like to do?\")\n",
" for i in range(len(options)):\n",
" print(f\"{i+1}: {options[i][0]}\")\n",
"\n",
"# This step does not have test"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HHSMmc2yUa3N"
},
"source": [
"# Step 30 - Certification Project 1"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Om-8B7mRTShM"
},
"source": [
"Now put it all together to build a multi-function calculator. Use the menu and the functions you created in the previous steps. Define one more function of your own. Create the menu so that the user input will run a function."
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {
"id": "vJruVEMwUWFX",
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"What would you like to do?\n",
"1: Simple calculations\n",
"2: Check if a number is prime\n",
"3: Prime factorize a number\n",
"4: Find simplified form of a square root\n",
"5: Solve an euqation for X\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
" 4\n",
"Input a radical/square root number (without symbol) to find it's simplified form: 32\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle 4 \\sqrt{2}$"
],
"text/plain": [
"4*sqrt(2)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Write your code here\n",
"\n",
"print_menu()\n",
"\n",
"try:\n",
" selected = int(input())\n",
"except BaseException as e:\n",
" print(\"Error:\", e)\n",
"if selected < 1 or selected > len(options):\n",
" print(\"Error: Invalid option selected\")\n",
"elif selected == 4: # for pretty printing sqrt symbol\n",
" display(options[selected-1][1]())\n",
"else:\n",
" options[selected-1][1]()\n",
"\n",
"# This step does not have test"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [
"szp5flp1fA8-",
"jVXoY1mmfBjG",
"EjxsoAEij2z5",
"yAR4qoEcj7tv",
"9n3Rf_u4kVRc",
"33FosVOV1NBu",
"rujSYyXZ3AOY",
"_NVz--_vqAxA",
"-lFO35p7l3cL",
"6PeVH4Gx3PE3",
"D5Z8_9gN39TD",
"rPT2OymY4HdE",
"yED2Dox44JYj",
"uQdfUUlw4Kfg",
"XEf8vCUH4Lc_",
"EyV2Udzs4MVS",
"76XeyVVM4NrX",
"B4WZJ0Iv4O1d",
"1hru9M7J4P48",
"cG_JMTeb4Q-E",
"a6Kr7wKq4SBP",
"IIb5MHzI4TAU",
"4VnLdwgZ4WHz",
"cVC_ENRHfIP4",
"Llx2Kaau8Dsz",
"tMa9hDVL85sl",
"d1WVETzF9-Dv",
"xmlkOkEc-iqL",
"98oHO4mh4X3e",
"xtI0kyR3RpFw",
"lgqRbYTWTIst",
"HHSMmc2yUa3N"
],
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}