0% found this document useful (0 votes)
5 views

ENGG1003 Lab08 PythonBasics 2425T2

Lab 08 of the ENGG1003 course focuses on Python basics, including setting up Python, saving work, and practicing variables and expressions. Students will complete tasks involving input/output statements, decision trees, and submitting their work via Blackboard. The lab emphasizes practical coding exercises to enhance digital literacy and computational thinking skills.

Uploaded by

wongchinghoi32
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

ENGG1003 Lab08 PythonBasics 2425T2

Lab 08 of the ENGG1003 course focuses on Python basics, including setting up Python, saving work, and practicing variables and expressions. Students will complete tasks involving input/output statements, decision trees, and submitting their work via Blackboard. The lab emphasizes practical coding exercises to enhance digital literacy and computational thinking skills.

Uploaded by

wongchinghoi32
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

ENGG1003

Digital Literacy and


Computational Thinking - P

Lab 08 Python Basics


2024-25 Term 2
Setup Python on Your
Computer
(Recap)
 Download and follow the steps on
Blackboard
 Lab 01 -
InstallationGuide_python_Win11_macOS.pptx
 Windows users: slides 1 – 6
 macOS users: slides 7 – 15

 You may also work on your VM !


Save Your Work Properly
(Recap)
 Setup your own filing system: create a folder
for your works in ENGG1003
 Keep on your own computer, e.g., Documents
 Keep on portable storage such as USB drive
 Keep on cloud storage such as OneDrive
(CUHK O365)

 Folder structure is hierarchical, i.e., tree-like


with branches called sub-folders
Lab 08 Activities

 Practice using variables and expressions interactively

 Use of basic python commands


 input(), data types such as int() and float()
commands

 Solving problems using if: elif: else: with Bonus

 Upload and submit your saved files to Blackboard


Variables and Expressions
temperature = 24.6
humidity = 0.75 Key-in OR copy-and-
print(temperature > 24) paste
print(humidity > 0.85) one line at a time!
 Practice on print(temperature >= 24 or humidity < 0.7)
print(22 < temperature < 28)
IDLE (Python)
approx_dew_point = temperature - (100 - humidity * 100) /
print('Dew point =', approx_dew_point)
Execute a batch of codes
 Feel troublesome?
 If you have a batch of codes to be executed (like the
codes in the previous slide, it is good to create a
python (program) file by Python IDLE.
 Start up the Python IDLE, you will see the Python
Shell environment.
This is Python Shell
environment. We can
enter and execute
code one-by-one only.
Execute a batch of codes
 Click on "File" from menu ribbon, then click "New
File", a new window with the name "untitled" will
be prompted.
 This is the Python editor. We can type in a batch
of codes here and execute them together. It will
help to organize the execution of codes.

This is Python editor.


We can enter and
execute a series of
codes together.
Execute a batch
of codes
 Try to type in the example codes to the
editor.
 Click Run  Run Module, or click [F5] key.
 Python editor request to save the file (.py)
before execution. Let’s save the file as
"Lab08Example.py" [F5] : Execute the
python
 The Python IDLE Shell will show the (Lab08Example.py) file
output(s) of the corresponding python
program file "Lab08Example.py" in IDLE
Shell.
 This is convenience to make corrections
that if the codes contain any mistakes.
 Enjoy your programming~!
TASK 0
Declarations
Task 0 – Download the
template
 Download the Python code template,
Lab08_2425T2_template.py from Blackboard

 Save the file as Lab08.py in your folder or


workspace

 To open the Lab08.py:


 Launch IDLE Python, File  Open  Browse to your
workspace, and pick Lab08.py  Open.

 Read the comments in the file, and modify the code


accordingly
Lab08_2425T2_template.p
y All
All the
the lines
lines starting
starting with
with #
# or
or
embedded between """ are
comments

In between the comments and codes,


you can remove the … and add your
Python codes

To run the codes,


click Run  Run Module [F5]
Task 0 - Declarations

Change the student name,


SID, class/section, and date
to complete the declaration
TASK 1
Input & Output
Task 1 – Input and Output

 In this task, we experience the use of input and output


statements
 Ask the user to input his/her surname and student ID
 Output the information in the same format as in the
sample outputs on the next slide

 Notes:
 No input validation is required for any of the tasks in this
lab exercise.
 No matter whether the surname is entered in uppercase or
lowercase letters, it should be printed with the first letter
capitalized and the rest in lowercase.
 Hints: Use str.title() to print the surname
Task 1 – Sample Outputs

Sample Output 1.1 Sample Output 1.2

Task 1 - Input and output Task 1 - Input and output


Surname? chan Surname? CHAN
SID? 1155123456 SID? 1155123456
Welcome, Chan Welcome, Chan
Your SID is 1155123456 Your SID is 1155123456

Sample Output 1.3 Sample Output 1.4

Task 1 - Input and output Task 1 - Input and output


Surname? au yeung Surname? aU yeuNG
SID? s1155123456 SID? 1155123456
Welcome, Au Yeung Welcome, Au Yeung
Your SID is s1155123456 Your SID is 1155123456
Task 2
Sleep Habit
Task 2 – Sleep Habit

 In this task, we comment on the user’s sleep habit


based on the number of sleep hours per day

 Ask the user to input a real number of sleep hours


per day (e.g., S) with float type
 If S is less than 8 hours, it is not enough.
 Otherwise, it is enough.

 Output the information in the same format as in


the sample outputs
Task 2 – Example Codes

 Example of if…else statement

if x == 0 and y > 100:


print("Case 1")
else:
print("Others")
Task 2 – Sample Outputs

Sample Output 2.1 Sample Output 2.2

Task 2 - Sleep Task 2 - Sleep


How many sleep hours How many sleep hours
per day? 5 per day? 5.5
Too bad, 5.0 hours is Too bad, 5.5 hours is
not enough. not enough.

Sample Output 2.3 Sample Output 2.4

Task 2 - Sleep Task 2 - Sleep


How many sleep hours How many sleep hours
per day? 8 per day? 9.9
Good, 8.0 hours is Good, 9.9 hours is
enough. enough.
Task 3
Diet Habit
Task 3 – Diet Habit

 In this task, we predict the dramatic future of the


user based on his/her diet habits

 Ask the user to input the number of meals per day


(e.g., M) and the amount of vegetables in grams per
meal (e.g., V)

 Generate your prediction according to the decision


tree on the next slide

 Output the information in the same format as in the


sample outputs
M for Meals

Task 3 – Decision Tree V for


Vegetables

M < 3?

Yes No
V<
M > 5?
150?

Yes No Yes No

V< V<
Salted 150? 150?
Monkey
Fish

🐟 🐒 Yes No Yes No

Weak Healthy
Pig Elephant
Human Human
🐖 🐘 😪 😃
Task 3 – Example Codes

 Example of if…elif…else statement

if x == 0 and y > 100:


print("Case 1")
elif x != 20 or y <= 10:
print("Case 2")
else:
print("Others")
Task 3 – Sample Outputs

Sample Output 3.1 Sample Output 3.2

Task 3 - Diet Task 3 - Diet


Meals per day? 1 Meals per day? 2
Vegetable(g) per meal? 100 Vegetable(g) per meal? 150
By keeping this diet, you By keeping this diet, you
will probably become a will probably become a
Salted Fish. Monkey.

Sample Output 3.3 Sample Output 3.4

Task 3 - Diet Task 3 - Diet


Meals per day? 3 Meals per day? 5
Vegetable(g) per meal? 100 Vegetable(g) per meal? 200
By keeping this diet, you By keeping this diet, you
will probably become a Weak will probably become a
Human. Healthy Human.
Task 3 – Sample Outputs

Sample Output 3.5 Sample Output 3.6

Task 3 - Diet Task 3 - Diet


Meals per day? 6 Meals per day? 7
Vegetable(g) per meal? 100 Vegetable(g) per meal? 150
By keeping this diet, you By keeping this diet, you
will probably become a Pig. will probably become an
Elephant.
Task 4
Hungry?
Bonus Task 4 – What if you
are hungry?
 In this task, we implement a decision tree to determine
what you should do when you are hungry
 Ask the user a few yes/no questions step-by-step:
 Hungry (Y/N)?
 If the user is hungry, ask: Any foods around (Y/N)?
 If there's food available, ask: Is it fresh? (Y/N)?
 If the user types either "y" or "Y", we will interpret it as
"Yes". Otherwise, we assume the answer is "No", even
if the user typed in "Yes".
 Generate your decisions according to the decision tree
on the next slide
 Output the information in the same format as in the
sample outputs
Bonus Task 4 – What if you
are hungry?
Hungry
?

No Yes
Keep
Foods?
working
hard then.
Yes No

Fresh? Take a
good rest.

Yes No

Don’t eat
Eat it!
it!
Bonus Task 4 – What if you
are hungry?
Sample Output 4.1 Sample Output 4.2

Task 4 - Hungry Decision Task 4 - Hungry Decision


Tree Tree
Hungry (Y/N)? n Hungry (Y/N)? y
Keep working hard then. Any foods around (Y/N)? n
Take a good rest.

Sample Output 4.3 Sample Output 4.4

Task 4 - Hungry Decision Task 4 - Hungry Decision


Tree Tree
Hungry (Y/N)? y Hungry (Y/N)? y
Any foods around (Y/N)? Y Any foods around (Y/N)? y
Is it fresh (Y/N)? Y Is it fresh (Y/N)? N
Eat it! Don't eat it!
Bonus Task 4 – What if you
are hungry?
Sample Output 4.5 Sample Output 4.6

Task 4 - Hungry Decision Task 4 - Hungry Decision


Tree Tree
Hungry (Y/N)? Yes Hungry (Y/N)? y
Keep working hard then. Any foods around (Y/N)? Yes
Take a good rest.
Submission

 Once you complete the tasks, upload Lab08.py file


to Blackboard

 Make sure the submitted file Lab08.py contains


your complete work, NOT the template file
Lab08_2425T2_template.py.

You might also like