{ "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 (if a cell is not in edit mode, double click it):\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "Your name: \n", "\n", "Your student ID:\n", " \n", "
" ] }, { "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). 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 \n", "- Submit the zip file of the report folder to the Moodle. The submission due date is ** 10 Oct 2023, 11:55 PM**" ] }, { "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": [ "" ] }, { "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": [ "" ] }, { "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": [ "" ] }, { "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