Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
31 views28 pages

CO Lab 2

This document contains notes from a computer organization lab session. It discusses decision making and algorithms, providing examples of pseudocode and flowcharts for a program that checks if a number is positive or negative. It also covers Python syntax basics like indentation, conditional statements using if/else, logical and comparison operators. Programming tasks are assigned, including writing code to check if a number is even or odd, calculate grocery bill totals, and determine if three numbers form a right triangle. The document concludes with a discussion of condition nesting using nested if/else statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views28 pages

CO Lab 2

This document contains notes from a computer organization lab session. It discusses decision making and algorithms, providing examples of pseudocode and flowcharts for a program that checks if a number is positive or negative. It also covers Python syntax basics like indentation, conditional statements using if/else, logical and comparison operators. Programming tasks are assigned, including writing code to check if a number is even or odd, calculate grocery bill totals, and determine if three numbers form a right triangle. The document concludes with a discussion of condition nesting using nested if/else statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Computer Organization

Lab #2
Dr Sajid Gul Khawaja
LE Kashaf Raheem
QUIZ #1

2
Algorithms and Decision
Making
Aim
Understand the importance and use of
decision making in problem solving

3
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

› What are some real-life examples?


– Walking around campus
(construction!)
– Choosing where to eat lunch

4
Decision Making: Pseudocode

PROBLEM SOLUTION
› Answer the question “Is a number PositiveNumberCheck
positive?” 1. Display “Enter the number: ”
2. Get the number (call it num)
3. If num > 0
4. Display “It is positive”
5. Else
6. Display “It is negative”
End

6
Decision Making: Flowchart

Display “Enter Get the


Start the number: ” number

TRUE FALSE
num > 0
Display Display
“It is positive” “It is negative”

End 7
Class Activity
› Design an algorithm that checks if entered number is even or
odd

8
Python Syntax

9
A Word about Python IDEs
› There are a few Python IDEs that you can choose from

› Each IDE offers a set of features while some features maybe absent

› Choosing an IDE to work boils down to a matter of personal preference

› Some Python IDEs:


– IDLE
– PyCharm
– Spyder/Anaconda
– Microsoft Visual Studio

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

› Lines of code that begin a block (often) end in a colon (:)


– The first line with less indentation is outside the block
– The first line with more indentation is inside the block

› A newline (Return) is used to indicate the end of a single line


of code
Conditional Statements
if (condition):
statements
else:
statements

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
if b > a: b = 200
print("b is greater than a") # you if b > a:
will get an error print("b is greater than a") # you will
get an error

13
Conditional Operators
Operator Example Meaning
> (A > B) A is Greater than B
< (A < B) A is Less than B
== (A == B) A is Equal to B
!= (A != B) A is Not Equal to B

› Example: Write a python script which identifies whether the


entered number is odd or even?

14
“Is a number positive?”: Python Code

PSEUDOCODE PYTHON CODE:


› PositiveNumberCheck › n = input("Enter the number")
› Display “Enter the number: ” › if float(n)>0:
› Get the number (call it num) › print("positive")
› If num > 0 › else:
› Display “It is positive” › print("negative")
› Else
› Display “It is negative”
› 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

EVEN/ODD PROBLEM PYTHON CODE

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 Operation Number Operation
asks user to select one of the 0 Addition
following operations and 1 Subtraction
performs the said operation 2 Multiplication
on 2 inputs provided by user 3 Division

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 Unit Price Quantity Multiple


Eggs (dozen) 300 Dozen
Bread 200 Single
Butter 400 Single
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 F: <60 - 0


B: <90 - 80 D: <70 - 60

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% to 50% Pleasant
35 Day
Less than 25 Less than 50% Cool Day

26
Condition Nesting

27
What is Nested if/else
Example Code
> There may be a situation when › var = 100
› if expression1:
you want to check for another › if var < ›200:statement(s)
condition after a condition – print "Expression value is less than 200"
› if expression2:
resolves to true. – if var == 150:
> In such a situation, you can use › ›print "Which
statement(s)
is 150"
– elif var == 100:
the nested if construct. › elif expression3:
› print "Which is 100"
› == statement(s)
– elif var 50:
› print "Which is 50"
› else:
– elif var < 50:
› ›print "Expression
statement(s)
value is less than 50"

› else: › else:
– print› "Could not find true expression"
statement(s)
› print "Good bye!"
28
Example: Nested if/else

QUESTION SOLUTION
› Write a program that gets an num = float(input("Enter a number:
input number and check if the "))
number is positive or negative or if num >= 0:
zero and display an appropriate if num == 0:
message using nested if/else print("Zero")
statement else:
print("Positive number")
else:
print("Negative number")

29
Next week: Repetition
Operators!
Advice:
Install latest version of IDLE on your laptops
Link: https://www.python.org/downloads/
Or VS Code and integrate python

30

You might also like