Lab 02
Lab 02
Lab #2
Dr Asad Mansoor Khan
LE Kashaf Raheem
LAB QUIZ #1
Write a python program which
calculates and displays the total surface
area of a trapezoid. All the values in the
formula should be taken as input from
the user.
• Formula:
𝑇𝑜𝑡𝑎𝑙 𝑆𝑢𝑟𝑓𝑎𝑐𝑒 𝐴𝑟𝑒𝑎
= 𝐴 + 𝐵 + (𝑃𝑒𝑟𝑖𝑚𝑒𝑡𝑒𝑟 × 𝐻𝑒𝑖𝑔ℎ𝑡)
2
Algorithms and
Decision Making
Aim
Understand the importance and use of decision making in problem
solving
4
Decision Making
• Selecting one choice from many
based on a specific reason or
condition
If something is true, do A … if
it’s not, do B
5
Decision Making: Pseudocode
Problem Solution
Answer the
PositiveNumberCheck
1. Display “Enter the number: ”
a number
3.
positive?”
5. Else
6. Display “It is negative”
End
6
Representation: Decision Making
7
Decision Making: Flowchart
Display “Enter Get the
Start the number: ” number
TRUE FALSE
num > 0
Display Display
“It is positive” “It is negative”
End
8
Class Activity
Design an algorithm
that checks if
entered number is
even or odd.
9
Python Syntax
10
Python Basics
• Indentation controls everything
A complete lack of braces {} to indicate code blocks
Instead, indentation indicates the start and end of a
code block
if a >= 0:
print(“a is a positive number”)
else:
print(“a is a negative number”)
12
Indentation and Conditional
Statements!!!
Incorrect Version Correct Version
a = 33 a = 33
b = 200 b = 200
if b > a: if b > a:
print("b is greater than a") # you will print("b is greater than a") # you will get
get an error an error
13
Conditional Operators
14
“Is a number positive?”: Python Code
• Else
• End
Improved Version
• n = input("Enter the number")
• if float(n)>0:
• print("positive")
• elif float(n)==0:
• print("zero")
• else:
• print("negative")
16
Class Activity
• If num % 2 == 0 • else:
• Else
• End
17
Tasks
Make sure to comment your code.
Use appropriate variable name
Know the location where file is being saved
Create different files for different tasks
18
Task 1:
Write a python
script that asks Operation Operation
user to select one of Number
operation on 2 3 Division
inputs provided by
user.
19
Task 2:
Modify last weeks grocery bill task such
that the cashier enter type of item and its
quantity to get the payment. Program
allows getting payment per item.
Item Type Item Unit Price Quantity
Multiple
0 Eggs (dozen) 300 Dozen
1 Bread 200 Single
2 Butter 400 Single
3 Jam 400 Single
20
Logical Operators
Operator Example Meaning
and (A > B) and (A != 0) A is Greater than B and A is not equal
to zero
or (A > 0) or (A == 0) A is Greater than or equal to zero
Input Validation
Input validation is the process of analyzing inputs and disallowing
those which are considered unsuitable.
21
Example (including input validation)
Write a program that takes a
student's score as input and prints
grades as A, B, C, D, or F
depending upon the thresholds
given below.
A: 100 - 90 C: <80 - 70
B: <90 - 80 D: <70 – 60
F: <60 - 0
22
Tasks
Make sure to comment your code.
Use appropriate variable name
Know the location where file is being saved
Create different files for different tasks
23
Task 3:
Design an algorithm and Write a
program that takes three nonzero
integers as input and determines and
prints whether they’re the sides of a
right triangle.
24
Task 4:
Draw flowchart and Write a
program that inputs temperature and
value of humidity from user and
display related forecast.
Temperature Humidity Forecast
Greater than 35 50% to 60% Hot Day
Between 25 and 35 35% to 50% Pleasant Day