Lab 3 - Handout
Lab 3 - Handout
Goal
Tasks
Deadline:
You are very welcome to ask clarifica�on ques�ons to TAs. But please read the document
carefully before you ask ques�ons.
It is an academic offense to copy code from other students, provide code to other students, let
other people (including the teaching staff) debug your code, or debug other students’ code.
Ques�ons 3 – 5 may not be arranged by the difficulty level. You are highly recommended to
take a look and decide the order of comple�ng the ques�ons. You will experience something
similar and it is important to learn how to triage your tasks.
Let’s go through this example together. Suppose we define a new opera�on # between two
natural numbers x # y such that it results in x2 – y2. For example:
2 # 3 = 22 – 32 = 4 – 9 = -5
Our goal is to write a script that prompts the user to give us expressions in the a # b format
and our script will calculate the value and print the result. For example:
You can take a few minutes to think of how you would approach this problem, or even write
some code yourself.
Now let’s do it together. If you are able to finish the script yourself, awesome! But please s�ll
along as I will introduce computa�onal thinking so that it will benefit you in the future if we have
more complicated tasks. If you are not able to finish the script yourself, no worries! Let’s see how
to approach this problem in a more systema�c manner.
So how do we write a script to achieve the func�onality? Recall that one of the principles we
men�oned in the first lecture is that we use building blocks to build systems. Let’s break down
the problem. Please note that you can use the same procedure for complicated problems in the
future.
Recall that in the previous lab, your code usually has the following the structure:
Similarly, our script, or most of the scripts, also follow this structure. The challenging part of this
problem is how to further break down the calcula�on part. Pause here before you read on, and
think about what steps are needed.
1. Find 2 and 3 in “2 # 3”
2. Calculate the result based on the defini�on of #
You are highly recommended to atempt the ques�ons yourself before you look at the solu�on
as a way to learn.
In the string “2 # 3”, how we find the numbers is to see what are the numbers before
and a�er the ‘#’. So here are the steps we need:
1. Find 2 and 3 in “2 # 3”
a. Find where ‘#’ is
b. The number before ‘#’ is 2
c. The number a�er ‘#’ is 3
2. Calculate the result based on the defini�on of ‘#’
Now let’s put it all together, the structure of our script should look like if our example is “2 # 3”:
Our goal here is to write a script that can handle any expressions in this format, not just “2 # 3”.
So let’s generalize our structure:
Solu�on:
There is nothing so special about our specific example “2 # 3”, we do not need to make
adjustment. Note that this may not be true to any arbitrary example, so you need to be
careful. For example, when you implement the division opera�on, you may choose the
example “4 / 2”. However, there are something special about this example that 4 can be
perfectly divided by 2, unlike “5 / 2” which end up as a float.
Each line of comment shows the goal of the chunk of code below it. And now we just need to
handle each mini problem. Let’s work on each goal.
Solu�on:
This is very similar to what we did in the previous lab, and it is straigh�orward:
Solu�on:
The data type of “x # y” is a string. You can use help(str) in the interac�ve mode to
find if there is any method that is useful here. You may no�ce this method:
It will give us the index of a single character or a string. This is what we need:
Solu�on:
Here we need the substring before the ‘#’ symbol, so we need to use string slicing
Note:
Solu�on:
Solu�on:
Solu�on:
Solu�on:
There is a bug because we got a substring, but we need integers for the calcula�on.
Note on debugging:
• It is useful to show the line numbers as the interpreter will tell us exactly which line has
the error and it is easier to locate the bug. In order to show the line number in the Wing
IDE, go to edit -> Show Line Numbers.
Solu�on:
This is a very typical bug when working with strings. You should be very careful about indices.
For example, when using the example “2 # 3”, the index of ‘#’ is 2. So in “2 # 3”[2:], it is a
substring that starts from the # (including #).
As seen in this example, your computer will execute whatever you implement. It is exactly the
same as the making sandwich video showed in the first lecture. If you cannot fix the bug by
looking at the code, use the debugger to help you! The instruc�ons can be found in the previous
lab.
Here is a summary of what we did. We first break down the problem into smaller pieces. If it is a
bit challenging to do so, you can always use a specific example to guide you and then generalize
your steps. However, you need to be careful and think about whether this is anything special
about your example. You can then treat each step as your sub-goals and solve each mini
problem. Lastly, you may need to perform some debugging.
Submission
• Go to eClass -> our course -> Week 4 -> Prac�ce -> Lab -> Lab 3 Submission
Rubrics:
• You will get full mark for this ques�on if you submit the solu�on (with no typo) to the
required loca�on on Prairielearn before the deadline.
Note:
Let’s define a new logical operator xor. It can be used between two Boolean literals a xor b. It
is true if and only if one of a and b is True. Here is the Truth table of this logical operator.
a b a xor b
True True False
True False True
False True True
False False False
The code to debug (lab3_task2.py) may have syntax errors, run-�me errors, and seman�c errors.
To help with debugging, you can use the debugger to execute the code line by line (you should
fix the syntax errors before doing this).
Note: You can use bool to turn the str ‘1’ to True, and ‘’ (empty string) to False. For example,
bool(‘1’) = True
Requirement
• Your submission must be based on the provided code. There are simpler ways of doing
this, but you cannot delete all the provided code and write your own.
• You can only use logical operators (e.g., and, or, not), assignment, bool(), and print
statement. You cannot use other statements/libraries, to name a few (It’s ok if you don’t
know what they are):
o You are not allowed to import other libraries
o You are not allowed to use if-statement
o You are not allowed to use comparison operators such as == or !=
o You are not allowed to use arithme�c opera�ons such as +, -, etc.
o You are not allowed to use bitwise opera�ons
Submission
Copy the Python code to Lab 3 -> Task 2 on Prairielearn.
You can resubmit your code as many �mes as you need, but you need to wait for 5 minutes
before submission (You need spend some �me debugging!).
Note:
• You will not receive any mark if you do not submit it to the designated loca�on on
Prairielearn before the deadline
• You will not receive any mark if your code violates the above requirements
• Otherwise
o You will receive 7.5 pts if your script correctly provide solu�on True xor True,
which is input 1 for a and input 1 for b
o You will receive 7.5 pts if your script correctly provide solu�on True xor False,
which is input 1 for a and no input for b (press enter directly)
o You will receive 7.5 pts if your script correctly provide solu�on False xor True,
which no input for a (press enter directly) and input 1 for b.
o You will receive 7.5 pts if your script correctly provide solu�on False xor False, ,
which no input for a (press enter directly) and no input for b (press enter
directly)
If a child needs to buy a �cket, your script should show True, otherwise show False.
Requirement
• You must not use if-statement (It’s ok if you don’t know what it is, as it indicates that it is
very unlikely for you to use it)
Submission
Copy the Python code to Lab 3 -> Task 3 on Prairielearn.
You can resubmit your code as many �mes as you need, but you need to wait for 5 minutes
before submission (You need spend some �me debugging!).
Note:
Rubrics:
• You will not receive any mark if you do not submit it to the designated loca�on on
Prairielearn before the deadline
• You will not receive any mark if your code violates the above requirements
• Otherwise
o You will receive 3 pts if your script correctly provide solu�on to our example
(i.e., age = 3, height = 100, show False)
o You will receive 2 pts if your script correctly provide solu�on to our example
(i.e., age = 4, height = 146, show True)
o You will receive 1.5 pts if your script correctly provide solu�on to another
example (e.g., another case 1)
o You will receive 1.5 pts if your script correctly provide solu�on to another
example (e.g., another case 2)
o You will receive 2 pts if your script correctly provide solu�on to an example with
random input
However, the user may input a number with trailing spaces, which may appear at the beginning,
or end of the phone number, or both. For example, “ 1234567890”, “1234567890 ”,
“ 1234567890 ”.
Moreover, Users may also include the symbol ‘-’, ‘(‘, ‘)’ and spaces in the middle of their phone
numbers, such as “(123) 456 - 0000” or “123 - 456 - 7890” or “123 456 7890”or “12- 3456-78-
90” or “1)234(56 – 789(0” (although the last case looks weird, but in our case, we accept it for
simplicity)
Your script should correctly iden�fy those input are valid phone numbers (i.e., show True) and
show False otherwise.
Hint:
• You may need various string methods, especially the ones in the puzzles.
• If the code does not work as you planned, you can use the debugger to figure out the
problem.
• If you s�ll cannot figure out the problem with a debugger, try to iden�fy the problem
and isolate/abstract the problem in a different script and ask for help from the TA.
• If you have trouble iden�fying the problem and/or isola�ng the problem, you can ask
the TA how to do so. (Note: Your TA cannot debug the code for you)
Requirement
• You must not use the if-statement (It’s ok if you don’t know what it is, as it indicates that
it is very unlikely for you to use it)
Submission
Copy the Python code to Lab 3 -> Task 4 on Prairielearn.
You can resubmit your code as many �mes as you need, but you need to wait for 5 minutes
before submission (You need spend some �me debugging!).
Note:
• You will not receive any mark if you do not submit it to the designated loca�on on
Prairielearn before the deadline
• You will not receive any mark if your code violates the above requirements
• Otherwise
o You will receive 1 pt if your script correctly provide solu�on to
“ 1234567890”
o You will receive 1 pt if your script correctly provide solu�on to
“ 1234567890 ”
o You will receive 1 pt if your script correctly provide solu�on to “(123) 456 -
0000”
o You will receive 1 pt if your script correctly provide solu�on to “123 - 456 - 7890”
o You will receive 1 pt if your script correctly provide solu�on to “e31235h62i”
o You will receive 5 pts if your script correctly provide solu�on to other examples
For example, my first name is Meiying, and last name is Qin. Then the script should print out:
Qin, Meiying
You script should take the following input from the user:
• First name
• Last name
Requirement
• The input must be in the order of first name, and then last name.
• Your code should not take any other input
• The print statement should be the last line of your code
• Make sure you just print the value, and no other texts
• You should remove other print statements
Submission
Copy the Python code to Lab 3 -> Task 5 on Prairielearn.
You can resubmit your code as many �mes as you need, but you need to wait for 5 minutes
before submission (You need to spend some �me debugging!).
Rubrics
• You will not receive any mark if you do not submit it to the designated loca�on on
Prairielearn before the deadline
• You will receive 2 pts if your script correctly provide solu�on to our example (i.e., with
the input listed above, your script should output Qin, Meiying)
• You will receive 1 pts if your script correctly provide solu�on to another example
• You will receive 1 pts if your script correctly provide solu�on to a slightly more
challenging example
For example, my first name is Meiying, and last name is Qin. Therefore, I should provide the
script the following input:
Qin, Meiying
Qin
Requirement
• The input must be in the format of last name, comma, one space, and first name and
should take them as one input.
• Your code should not take any other input
• The print statement should be the last line of your code
• Make sure you just print the value, and no other texts
• You should remove other print statements
Submission
Copy the Python code to Lab 3 -> Task 6 on Prairielearn.
You can resubmit your code as many �mes as you need, but you need to wait for 5 minutes
before submission (You need to spend some �me debugging!).
Rubrics
• You will not receive any mark if you do not submit it to the designated loca�on on
Prairielearn before the deadline
• You will receive 4 pts if your script correctly provide solu�on to our example (i.e., with
the input listed above, your script should output Qin)
• You will receive 2 pts if your script correctly provide solu�on to a slightly more
challenging example
• You will receive 2 pts if your script correctly provide solu�on to another slightly more
challenging example
For example, my first name is Meiying, and last name is Qin. Therefore, I should provide the
script the following input:
Qin, Meiying
Meiying
Requirement
• The input must be in the format of last name, comma, one space, and first name and
should take them as one input.
• Your code should not take any other input
• The print statement should be the last line of your code
• Make sure you just print the value, and no other texts
• You should remove other print statements
Submission
Copy the Python code to Lab 3 -> Task 7 on Prairielearn.
You can resubmit your code as many �mes as you need, but you need to wait for 5 minutes
before submission (You need to spend some �me debugging!).
Rubrics
• You will not receive any mark if you do not submit it to the designated loca�on on
Prairielearn before the deadline
• You will receive 4 pts if your script correctly provide solu�on to our example (i.e., with
the input listed above, your script should output Meiying)
• You will receive 2 pts if your script correctly provide solu�on to a slightly more
challenging example
• You will receive 2 pts if your script correctly provide solu�on to another slightly more
challenging example