Assignment 1
Assignment 1.1: BMI Calculator
Task
Create a BMI calculator program which prompt user to input their height and weight.
First, the program will ask the user “Enter your height in meters: “.
Second, the program will ask the user “Enter your weight in kg: “.
Then, the program will show “Your BMI is: “ followed by BMI index.
The BMI of a person can be calculated using formula weight / (height ^ 2)
Example
[Python Console]
Enter your height in meters: 1.87
Enter your weight in kg: 60.9
Your BMI is: 17.42
Tips
-
Whitespace shown in the quote is important! Write carefully.
-
Use the appropriate data type for input and output.
Assignment 1.2: Self-study
Python functions
1. What do you think this piece of code do?
def add(a, b):
return a + b
2. What do you think is the output?
s = add(10, 30)
print(s)
3. What do you think is the output?
print( add(10, 30) )
4. What do you think is the output?
print( add(10, add(20, 30) ) )
5. What do you think this piece of code do?
def prod(*args):
result = 1
for number in args:
result = result * number
return result
6. In 5., When you write result = result * number
, is result
on the LHS the same with on the RHS?
- LHS = left hand side
- RHS = right hand side
Assignment 1.3: Self-study
Boolean operations
Boolean operations are similar to arithmetic operations, but in a logical way.
Remind that Boolean data types have only two possible value: True
or False
.
In logic, there is AND, OR, IMPLIES, and EQUIVALENCE operators.
In Python, these rules apply as well, and in similar fashion, the syntax (symbol) of these operators make sense!
A | not A | B | A and B | A or B |
---|---|---|---|---|
False | True | False | False | False |
False | True | True | False | True |
True | False | False | False | True |
True | False | True | True | True |
Comparison operators
There are several comparison operators
- Equal to
a == b
: returns True ifa
equalsb
. - Not equal to
a != b
- Strictly less than
a < b
- Less than or equal to
a <= b
- Strictly greater than
a > b
- Greater than or equal to
a >= b
(Please study the rest for your self, including how to use it in Python)