update,
This commit is contained in:
891
max015/_ref/T03/CDS1001T3Report.ipynb
Normal file
891
max015/_ref/T03/CDS1001T3Report.ipynb
Normal file
@@ -0,0 +1,891 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# CDS1001 Tutorial 3 Report"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
" \n",
|
||||
"#### Input your name and student ID in the cell below (<font color='red'>if a cell is not in edit mode, double click it</font>):\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<div class=\"alert alert-info\">\n",
|
||||
" \n",
|
||||
"Your name: \n",
|
||||
"\n",
|
||||
"Your student ID:\n",
|
||||
" \n",
|
||||
"</div>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Instructions for the report**:\n",
|
||||
"- Follow Section 1 and Section 2 of the tutorial instruction to launch Python IDLE through Anaconda Navigation.\n",
|
||||
"- Refer to Section 2.2 of the tutorial instruction to open tutorial 3 report\n",
|
||||
"- Complete Parts 1-4 led by the lecturer\n",
|
||||
"- Complete Part 5 independently\n",
|
||||
"- Follow Section 3 of the tutorial instruction to save the report and zip the report folder. The zip file is named as CDS1001T3Report{your student_id}.zip (e.g., if student_id is 1234567, then the zip file's name is CDS1001T3Report1234567.zip). <font color='red'>The zip file needs to include the following files:\n",
|
||||
" - an .ipynb file of this tutorial report \n",
|
||||
" - image files of flowcharts or screenshots used in this tutorial report </font> \n",
|
||||
"- Submit the zip file of the report folder to the Moodle. The submission due date is **<font color='red'> 10 Oct 2023, 11:55 PM</font>**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Part 1 Condition Expressions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 1.1. Execute the codes below, and explain meanings of the logic expressions in the if-statement (4 points):"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### (a)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"company_name = 'FE'\n",
|
||||
"if company_name == 'FE' or company_name == 'SF':\n",
|
||||
" print(company_name)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to explain the meaning of the expression above:_**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### (b)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"quantity = -10\n",
|
||||
"if quantity <= 0:\n",
|
||||
" print('Input must be non-negative.')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to explain the meaning of the expression above:_**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### (c)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"quantity = 150\n",
|
||||
"if not (quantity < 150 or quantity > 200):\n",
|
||||
" print(quantity)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to explain the meaning of the expression above:_**\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### (d)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"company_name = 'FE'\n",
|
||||
"quantity = 250\n",
|
||||
"if (company_name == 'FE' and quantity > 200) or (company_name == 'SF' and quantity >=150): \n",
|
||||
" print(company_name) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to explain the meaning of the expression above:_**\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 1.2. Explain how to express the following condition using logic expression (9 points):\n",
|
||||
"(1) Given a string variable `student_id` that stores a student's id, express the condition that the student is you.\n",
|
||||
"\n",
|
||||
"(2) Given an integer variable `age` that stores a user's age, express the condition that the user's age falls in an interval between 20 and 30 (including 20 and 30). \n",
|
||||
"\n",
|
||||
"(3) Given an integer variable `age` that stores a user's age, and a string variable `loc` that stores the user's location, express the condition that either the user is in Hong Kong and older than 20, or the user is younger than 40."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to explain your answer:_**\n",
|
||||
"\n",
|
||||
"(1) \n",
|
||||
"\n",
|
||||
"(2) \n",
|
||||
"\n",
|
||||
"(3)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Part 2 Conditional Statements"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 2.1. Execute the codes below, and explain meanings of the conditional statements (6 points):"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"##### (a)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"quantity=200\n",
|
||||
"threshold = 100\n",
|
||||
"priority = 0\n",
|
||||
"if quantity > threshold:\n",
|
||||
" priority = 1\n",
|
||||
"print(priority)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to explain the meaning of the conditional statements above:_**\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"##### (b)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"quantity=500\n",
|
||||
"threshold = 100\n",
|
||||
"priority = 0\n",
|
||||
"if quantity > 10*threshold:\n",
|
||||
" priority = 3\n",
|
||||
"elif quantity > 2 * threshold:\n",
|
||||
" priority = 2\n",
|
||||
"elif quantity > threshold:\n",
|
||||
" priority = 1\n",
|
||||
"print(priority)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to explain the meaning of the conditional statements above:_**\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"##### (c)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"quantity=500\n",
|
||||
"threshold = 100\n",
|
||||
"priority = 0\n",
|
||||
"if quantity > threshold:\n",
|
||||
" priority = 1\n",
|
||||
"elif quantity > 2 * threshold:\n",
|
||||
" priority = 2\n",
|
||||
"elif quantity > 10*threshold:\n",
|
||||
" priority = 3\n",
|
||||
"print(priority)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to explain the meaning of the conditional statements above:_**\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 2.2. Execute the code below, explain the errors received, and discuss how to fix the errors (6 points):"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"##### (a)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"quantity=200\n",
|
||||
"if quantity > 100:\n",
|
||||
"priority = 1\n",
|
||||
"print(priority)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to explain the errors of the code above, and discuss how to fix them:_**\n",
|
||||
"\n",
|
||||
"Error: no indentation before ``priority = 1``\n",
|
||||
"\n",
|
||||
"Correct: add indentation before it"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"##### (b)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"quantity='200'\n",
|
||||
"if quantity > 100:\n",
|
||||
" priority = 1\n",
|
||||
" if quantity > 200:\n",
|
||||
" priority = 2\n",
|
||||
" if quantity > 1000:\n",
|
||||
" priority = 3\n",
|
||||
"print(priority)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to explain the errors of the code above, and discuss how to fix them:_**\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"##### (c)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"quantity=200\n",
|
||||
"if quantity > 100 AND quantity < 300:\n",
|
||||
" print(priority)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to explain the errors of the code above, and discuss how to fix them:_**\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Part 3 Flowcharts"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 3.1. Write conditional statements for flowcharts below and explain their meanings (5 points)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"##### (a)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<img src=\"T3_flowchart_0.png\" width=\"300\">"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#Edit this cell to write conditional statements for the flowchart above:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to discuss the meaning of the flowchart above:_**\n",
|
||||
"\n",
|
||||
"if variable ``location`` is equal to 'Hong Kong', then print the message 'From Hong Kong'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"##### (b)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<img src=\"T3_flowchart_1.png\" width=\"500\">"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#Edit this cell to write conditional statements for the flowchart above:\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to discuss the meaning of the flowchart above:_**\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"##### (c)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<img src=\"T3_flowchart_2.png\" width=\"600\">"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#Edit this cell to write conditional statements for the flowchart above:\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
" **_Edit this cell to discuss the meaning of the flowchart above:_**\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 3.2. (Comparing Rates II). Company FE receives shipping rates from two carriers. Draw a flowchart and write a code to output which carrier provides a lower rate. (5 points)\n",
|
||||
"\n",
|
||||
"*Sample Input and Output:*\n",
|
||||
"\n",
|
||||
" Input:\n",
|
||||
" Enter the shipping rate of carrier 1: 10\n",
|
||||
" Enter the shipping rate of carrier 2: 20\n",
|
||||
"\n",
|
||||
" Output:\n",
|
||||
" Carrier 1 submits a lower rate of 10"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_(1) Edit this cell to insert your flowchart (see Slide 49 of Lecture 4 for how to write and save flowchart):_**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#(2) Edit this cell to write your code:\n",
|
||||
"\n",
|
||||
"rate1 = int(input('Carrier 1:'))\n",
|
||||
"rate2 = int(input('Carrier 2:'))\n",
|
||||
"if rate1<rate2:\n",
|
||||
" print('Carrier 1 has a lower rate of', rate1)\n",
|
||||
"else:\n",
|
||||
" print('Carrier 2 has a lower rate of', rate2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_(3) Execute and test your code above for various inputs in IDLE. Edit this cell to insert screenshots of your tests below:_**\n",
|
||||
" \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 3.3. (Payments). Write a program to prompt the user for hours and rate per hour to compute gross pay, for which you need to give the employee 1.5 times the hourly rate for hours worked above 40 hours. (5 points)\n",
|
||||
"\n",
|
||||
"(For the example below, 475 = 40\\*10 + (45-40)\\*10\\*1.5) \n",
|
||||
"\n",
|
||||
"*Sample Input and Output:*\n",
|
||||
"\n",
|
||||
" Input:\n",
|
||||
" Enter Hours: 45\n",
|
||||
" Enter Rate: 10 \n",
|
||||
"\n",
|
||||
" Output:\n",
|
||||
" Pay: 475"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_(1) Edit this cell to insert your flowchart:_**\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#(2) Edit this cell to write your code:\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_(3) Execute and test your code above for various input. Edit this cell to copy your test results:_**\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Part 4 Exception Handling"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 4.1. Test the codes below, given examples of the input that can lead to different outputs, explain meanings of the code (5 points):"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"##### (a)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"input_age = input('Enter your age: ')\n",
|
||||
"try:\n",
|
||||
" age = int(input_age)\n",
|
||||
" print('You were born in', str(2020-age))\n",
|
||||
"except:\n",
|
||||
" print('Error: input must be a number')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to describe inputs that lead to different outputs, and explain meanings of the code:_**\n",
|
||||
"\n",
|
||||
"Examples of Input and Output:\n",
|
||||
" \n",
|
||||
" \n",
|
||||
"\n",
|
||||
" \n",
|
||||
" \n",
|
||||
"Meaning of the code:\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"##### (b)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"input_number = input('Enter number of customers: ')\n",
|
||||
"number = int(input_number)\n",
|
||||
"quantity = 100\n",
|
||||
"try:\n",
|
||||
" average = quantity/number\n",
|
||||
" print('Average quantity is ', str(average))\n",
|
||||
"except:\n",
|
||||
" print('Input Error: input must be a positive number')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_Edit this cell to describe inputs that lead to different outputs, and explain meanings of the code:_**\n",
|
||||
"\n",
|
||||
"Examples of Input and Output:\n",
|
||||
" \n",
|
||||
" \n",
|
||||
"\n",
|
||||
" \n",
|
||||
" \n",
|
||||
"Meaning of the code:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 4.2. Explain when the following code will have runtime errors, and what error messages are. Use exception handling to revise the code, so as to avoid runtime errors. Test the code (5 points):"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"x = float(input ('Enter x:'))\n",
|
||||
"y = 1/x"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_(1) Edit this cell to explain when the code above will have runtime errors:_**\n",
|
||||
"\n",
|
||||
"Error 1: \n",
|
||||
"\n",
|
||||
"Error 2: "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#(2) Write the revise code in this cell:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_(3) Execute and test your revised code above for various input. Edit this cell to copy your test results below:_**\n",
|
||||
"\n",
|
||||
"Test 1:\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Test 2:\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 4.3. Rewrite the program in 3.3 using try and except so that your program handles non-numeric input gracefully.\n",
|
||||
"\n",
|
||||
"*Sample Input and Output:*\n",
|
||||
"\n",
|
||||
" Input:\n",
|
||||
" Enter Hours: 45\n",
|
||||
" Enter Rate: nine\n",
|
||||
" \n",
|
||||
" Output:\n",
|
||||
" Error, please enter numeric input\n",
|
||||
"\n",
|
||||
" Input:\n",
|
||||
" Enter Hours: forty \n",
|
||||
" \n",
|
||||
" Output:\n",
|
||||
" Error, please enter numeric input"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_(1) Edit this cell to explain when the code in 3.3 will have runtime errors, and how to avoid these errors:_**\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#(2) Edit this cell to write your code:\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_(3) Execute and test your revised code above for various input. Edit this cell to copy your test results below:_**\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Part 5 More Exercises"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 5.1. (Compare Rates III). Company FE receives shipping rates from three carriers. Draw a flowchart and write a code to output which carrier provides a lower rate. (25 points)\n",
|
||||
"\n",
|
||||
"*Sample Input and Output:*\n",
|
||||
"\n",
|
||||
" Input:\n",
|
||||
" Enter the shipping rate of carrier 1: 10\n",
|
||||
" Enter the shipping rate of carrier 2: 5\n",
|
||||
" Enter the shipping rate of carrier 3: 15\n",
|
||||
"\n",
|
||||
" Output:\n",
|
||||
" Carrier 2 submits the lowest rate of 5.0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_(1) Edit this cell to insert your flowchart (see Slide 49 of Lecture 4 for how to write and save flowchart):_**\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#(2) Edit this cell to write your code:\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_(3) Execute and test your revised code above for various inputs. Edit this cell to copy your test results below:_**\n",
|
||||
" \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 5.2. (Compare Rates of 3, Rewrite). Rewrite the program in 5.1 so that your program handles non-numeric input or non-positive input gracefully. (25 points)\n",
|
||||
"\n",
|
||||
"*Sample Input and Output:*\n",
|
||||
"\n",
|
||||
" Input:\n",
|
||||
" Enter the shipping rate of carrier 1: ten\n",
|
||||
" \n",
|
||||
" Output:\n",
|
||||
" Error, please enter positive numeric input\n",
|
||||
" \n",
|
||||
" Input:\n",
|
||||
" Enter the shipping rate of carrier 1: -10\n",
|
||||
" \n",
|
||||
" Output:\n",
|
||||
" Error, please enter positive numeric input\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_(1) Edit this cell to explain when the code in 5.1 will have runtime errors, and how to avoid these errors:_**\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#(2) Edit this cell to write your code:\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**_(3) Execute and test your revised code above for various inputs. Edit this cell to copy your test results below:_**\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.7"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
BIN
max015/_ref/T03/T3_flowchart_0.png
(Stored with Git LFS)
Normal file
BIN
max015/_ref/T03/T3_flowchart_0.png
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
max015/_ref/T03/T3_flowchart_1.png
(Stored with Git LFS)
Normal file
BIN
max015/_ref/T03/T3_flowchart_1.png
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
max015/_ref/T03/T3_flowchart_2.png
(Stored with Git LFS)
Normal file
BIN
max015/_ref/T03/T3_flowchart_2.png
(Stored with Git LFS)
Normal file
Binary file not shown.
Reference in New Issue
Block a user