Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

The Islamia University of

Bahawalpur
Department of
Information Security

Assignment # 3
Programming
Fundamentals
Class: BS(IS) – Semester 1
( Morning )
Course Instructor: Dr.
Shahzada Khurram
Name: Muhammad Talha
Munir
Roll no: F20BINFS1M02046
Q 1. Find the errors in the following if statements.

a) if x > 0 then print(x)


Result:
(a)
• if condition must be written
in braces ()
• After condition (x>0) must
write colon (:)
• After condition, the
statement of print (x) is written after
four spaces (by using tab) \t.
• Print statement is written in
double quotes (“ ”).
• In condition then is not
used.
b) if x = 1 :
y += 1
Result:
(b)
Solution:
If (x=1):
x = y+1
• If condition must be written
in braces ().
• After condition with colon
(:)
• Print statement is written
after four spaces (by using tab) \t.
• The operator of addition is
used to the right side y+1
c) xStr = input("Enter an integer value")
x = int(xStr) if xStr.isdigit() :
sum = sum + x
else :
print("Bad input for x")

Result

( c)

Solution:
x=int(input(“Enter an integer value”))
x=int(x)
if (x):
sum = sum + x
else
print(“Bad input of x”)

Q 2. Write pseudocode for a program that assigns letter grades for a quiz, according to the
following table:

Score Grade
90-100 A
80-89 B
70-79 C
60-69 D
< 60 F

Result:
1. Int(input(enter a score))
2. Condition if( x>=90 and x<=100) then print(‘A’)
3. condition elif (x>=80 and x<=89) then print(‘B’)
4. condition elif (x>=70 and x<=79 )then print(‘C’)
5. condition elif (x>=60 and x<=69) then print(‘D’)
6. condition elif (x<60 )then print(‘F’)
7. else:
8. print(‘strange grade’)

Q 3. Suppose the value of b is False and the value of x is 0. What is the value of each of the
following expressions?

a) b and x == 0
True
b) b or x == 0
False
c) not b and x == 0
False

d) not b or x == 0
True
e) b and x != 0
False
f) b or x != 0
False
g) not b and x != 0
False

Q 4. Complete the following truth table by finding the truth values of the Boolean expressions for
all combinations of the Boolean inputs a, b, and c.

a b c (a and b) or not c not (a and (b or not c))


False False False True True
False False True False True
False True False True True
False True True False True
True False False True False
True False True False True
True True False True False
True True True True False

Q 5. Simplify the following expressions. Here, b is a variable of type bool.

a) b == True

if(b)

b) b == False

if(!b)

c) b != True

if(!b)

d) b != False

if(b)

Q 6. Explain the difference between an if/elif/else sequence and nested if statements. Given of
example of each.

Def.

We can have a if...elif...else statement inside another if...elif...else statement. This is called nesting in
computer programming.
Any number of these statements can be nested inside one another. Indentation is the only way to figure
out the level of nesting. They can get confusing, so they must be avoided unless necessary.

Example:

''In this program, we check if the number is positive or negative or zero and display an appropriate
message'''

num = 3.4

# Try these two variations as well: # num = 0

# num = -4.5

if num > 0: print("Positive number")

elif num == 0: print("Zero")

else: print("Negative number")

Def.

Decision making is required when we want to execute a code only if a certain condition is satisfied.

The if…elif…else statement is used in Python for decision making

Example:

'''In this program, we input a number check if the number is positive or negative or zero and display

an appropriate message .This time we use nested if statement'''

num = float(input("Enter a number: "))

if num >= 0:

if num == 0:

print("Zero")

else:

print("Positive number")

else:

print("Negative number")

Q 7. Write a program in Python to calculate the total expenses. Quantity and price per item are
input by the user and discount of 10% is offered if the expense is more than 5000.

#input the quantity and price

quantity1 = int(input(“Enter a command”)


price1 = int(input(“Enter a command”)
price2 = int(input command)
total price= price1 + price2 + etc
if (total price>5000):
discount = total price – 500
print(“discount price”)

You might also like