INF110-Projects/Week-5/06_rounding.ipynb
cdez 140ffc6523 Remove the IPYNB Checkpoints, Did Week 5 HW
-- IPYNB Checkpoints
++ Git Ignore File for IPYNB Checkpoints
+- Week 5 Homework Files
2022-09-28 21:30:48 -07:00

163 lines
3.5 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Rounding with Function Calls"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These exercises give you practice calling functions in Python."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 1.** Using the function `round`, round the number 1.23456789 to the nearest integer. Give the result the name `rounded`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Round can take two parameters, the first is the number to round, while the second is the decimal places in which to round to.\n",
"rounded = round(1.23456789, 0)\n",
"rounded"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 2.** Round 1.23456789 to the nearest *thousandth* (.001).\n",
"\n",
"*Hint:* Running `round?` in a code cell will show you Python's documentation for the `round` function."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.235"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Using round, with the second parameter as three, you get three decimal point precision.\n",
"less_rounded = round(1.23456789, 3)\n",
"less_rounded"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Suppose you're counting all the small donations made to a political candidate. The donations were:\n",
"\n",
"* 32.25\n",
"* 15.00\n",
"* 24.45\n",
"\n",
"You need to report the total rounded to the nearest dollar.\n",
"\n",
"**Question 3.** First you decide to round *each donation* individually and then add up those 3 rounded amounts. Compute that."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"71.0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum_of_rounded_amounts = round(32.25, 0) + round(15.00, 0) + round(24.45, 0)\n",
"sum_of_rounded_amounts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 4.** You suspect this method doesn't work quite right. To check, you decide to try a different method: Adding up the donations, and then rounding the total. Compute that."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"72.0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rounded_sum = round((32.25 + 15.00 + 24.45), 0)\n",
"rounded_sum"
]
}
],
"metadata": {
"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.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 1
}