Tutorial 2 Worksheet: COMP1117A Computer Programming I 2018-2019
Tutorial 2 Worksheet: COMP1117A Computer Programming I 2018-2019
Tutorial objectives:
- To practice data manipulation.
- To practice the use of if-else statements.
- To be able to derive conditions to control branching of program flow.
- To practice the use of some built-in functions in the math library.
- First let’s tackle the first exercise a little by little. First, let’s we read an input and print it:
# Exercise 2.1
myNumber = int(input())
print(myNumber)
- The code above read from the input, convert it to integer,store it in a variable, then print it.
- Here we assume that we will be handling integer only but not a floating point value.
- To continue, instead of printing the number, we test if the number is positive or not:
if myNumber > 0:
print("Positive")
- Now we have written the first case, we will want to test it. With a if-statement added, there
will be at least two cases that you need to test, and the only case that we have handled is
positive numbers.
- Save and run the code, test your code with the value 1 and -1. You should get “Positive” for
input 1, and nothing for -1.
- Next, try to evaluate your code to see how well it is. We know that it can only handle one
case, so we expect that the code will not pass the auto-grader fully.
- Examine the result of evaluation, you will see that you failed 3 test cases, namely Negative,
Zero, and Negative One. You will see the input and expected output of these test cases if
you scroll down a bit.
- When using the auto-grader to evaluate your code, there are two things you should do:
- Run your code and test it with your own test cases first. This helps you to understand
your code.
- Read the result of evaluation carefully. The result tells you what the auto-grader is
testing and what the expected results are.
- With the code evaluated once and passed some of the tests, we know we are on the right
track. We then handle the second case. For instance, let’s handle the negative case first.
Adding right after the above if-clause:
- Save, run, and evaluate it, we have one more case to go.
- Let’s finish of by adding the else-clause right after the above elif-clause:
else:
print("Zero")
12 Positive
-1 Negative
0 Zero
Suppose the quadratic equation is ax2 + bx + c = 0 . The input to your program will be a , b ,
and c . Your program should then report the type of roots (Real, Double, Imaginary) of the
equation.
You can assume that input a , b , and c are integers and a will never be 0.
Note: Type of roots of a quadratic equation can be determined by its discriminant b2 − 4ac .
1 Real
-3
2
1 Double
2
1
1 Imaginary
2
4
For example:
Year 2016 is a leap year because it is divisible by 4 and is not divisible by 100;
Year 1900 is not a leap year although it is divisible by 4, because it is divisible by 100 and is
not divisible by 400;
Year 2000 is a leap year because it is divisible by 400.
- There are two possible results for this exercise. We may consider using a simple if-statement
to hand it, but then we need to come up with the correct condition:
# Exercise 2.3
year = int(input())
if ????????????????????:
print(year, "is a leap year.”)
else:
print(year, "is not a leap year.”)
- We can combine these conditions together with and, or, and parentheses:
- Instead of using a single condition, we can use multiple conditions. For example, we can
handle each conditions one by one and exhaust all possibilities.
1. Year is divisible by 4?
Yes → not sure, consider condition 2; No → must not be a leap year.
2. Year is divisible by 100?
Yes → not sure, consider condition 3; No → must be a leap year.
3. Year is divisible by 400?
Yes → must be a leap year; No → must not be a leap year.
if year % 4 != 0:
print(year, "is not a leap year.”)
elif year % 100 != 0:
print(year, "is a leap year.”)
elif year % 400 != 0:
print(year, "is not a leap year.”)
else:
print(year, "is a leap year.”)
- You should now see that there are many ways to solve a problem. Sometimes it will be
easier to break a condition into multiple ones to handle.
Sample Test Cases:
Input Expected output
3. Tutorial exercises
73 36
37
37 0
37
33 25
25
17
23 23
35
17
- In the formula, we need the value of π and actually we can use the built-in π value by
importing the math library.
- Add the following line on the top of your program. This tells Python that we are going to use
its math library.
import math
- Then you can use the built-in π value by the code math.pi. For example, you can just print
the value π with the following code.
print(math.pi)
- You will see 3.141592653589793 if you execute the above line of code.
- Next, when you finished the calculation. You can use another built-in function called
round(x,n) to round a number x to a specific number of digits n from the decimal point. For
example, you can just print the π value to 6 digits from the decimal point with the following
code.
print(round(math.pi, 6))
- You will see 3.141593 if you execute the above line of code.
- Now, please implement the formula and see whether you could pass the autograder. (Note:
we know that there is a built-in function in Python to convert degree to radian, please do not
use that function in this exercise.)
10 0.1745
10.5 0.1833
-123 -2.1468
- In the formula, we need the value of sin(x) and actually we can use the built-in sin(x) value
by importing the math library.
- Add the following line on the top of your program. This tells Python that we are going to
use its math library.
import math
- Then you can use the sin(x) value by the code math.sin(x). For example:
print(math.sin(math.pi/2))
π
- You will see 1.0 if you execute the above line of code. As in radian 2 is equal to 90
degree, and sine value of it is 1.
5 25.0
10
1.5708
5.8 19.241
6.7
1.4312
If we know the three sides (A, B, C) of the triangle, we can also calculate the area of triangle
using the Heron’s formula as follows:
√
A+B+C A+B+C
area = 2 ( 2 − A)( A+B+C
2 − B)( A+B+C
2 − C)
Write a program to ask the user to input the (float) values of A, B and C and print the area
rounded to 4 digits from the decimal point.
- In the formula, we need the value of √x and actually we can use the built-in sqrt(x) value by
importing the math library.
- Add the following line on the top of your program. This tells Python that we are going to
use its math library.
import math
- Then you can use the sqrt(x) value by the code math.sqrt(x). For example:
print(math.sqrt(4))
- You will see 2.0 if you execute the above line of code.
A+B+C
- Remarks: In the formula you can see that 2 is repeated many times, you may use a
A+B+C
temporary variable to store the computed value, e.g. temp = 2 . Then you can easily
manage and simplify the formula in your program.
10 43.3013
10
10
10.5 67.7351
15.75
12.985
- Please complete Exercise 2.1 to 2.8 on Moodle, make sure you have evaluated your code
and your code passed the auto-grader.
- Starting from this tutorial, you must evaluate your code on or before next Thursday
14:20 to get marks for the tutorial.