You are on page 1of 13

Programming Fundamentals (CS-116L) SSUET/QR/114

LAB#4

1
Programming Fundamentals (CS-116L) SSUET/QR/114

LAB # 04

DECISIONS

OBJECTIVE
To get familiar with the concept of conditional statement for simple decision making.

EXERCISE

A. Point out the errors, if any, and paste the output also in the following Python
programs.
1. Code
a = 500,b,c;
if ( a >= 400 ):
b = 300
c = 200
print( "Value is:", b, c )
Output:

2. Code
&number = eval(input("Enter an integer: "))
print(type(number))
if number % 5 == 0
2
Programming Fundamentals (CS-116L) SSUET/QR/114

print("HiFive")
else
print("No answer")

Output:

3. Code
if score >= 60.0
grade = 'D'
elif score >= 70.0
grade = 'C'
elif score >= 80.0
grade = 'B'
elif score >= 90.0
grade = 'A'
else:
grade = 'F'
Output:

3
Programming Fundamentals (CS-116L) SSUET/QR/114

B. What would be the output of the following programs:


1. Code
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
print("Hold the anchovies!")

Output

4
Programming Fundamentals (CS-116L) SSUET/QR/114

2. Code
num = 3
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")

Output

3. Code
age = 15

5
Programming Fundamentals (CS-116L) SSUET/QR/114

if age < 4:
price = 0
elif age < 18:
price = 1500
else:
price = 2000
print("Your admission cost is Rs" + str(price) + ".")

Output

C. Write Python programs for the following :

1. Any integer is input through the keyboard. Write a program to find out whether it is
an odd number or even number.

6
Programming Fundamentals (CS-116L) SSUET/QR/114

2. Write a program that asks for years of service and qualification from the user and
calculates the salary as per the following table:
Years of Service Qualifications Salary
>= 10 Masters 150,000
>= 10 Bachelors 100,000
< 10 Masters 100,000
< 10 Bachelors 70,000

7
Programming Fundamentals (CS-116L) SSUET/QR/114

3. Write an if-elif-else chain that determines a person’s stage of life, take input value for
the variable age, and then apply these conditions:

• If the person is less than 2 years old, print a message that the person is a baby.
• If the person is at least 2 years old but less than 4, print a message that the person is a
toddler.
• If the person is at least 4 years old but less than 13, print a message that the person is a
kid.
• If the person is at least 13 years old but less than 20, print a message that the person is
a teenager.
• If the person is at least 20 years old but less than 65, print a message that the person is
an adult.
• If the person is age 65 or older, print a message that the person is an elder.

8
Programming Fundamentals (CS-116L) SSUET/QR/114

Practice Codes LAB#4


1: Using ELIF
Letter = input()
If letter ==”a” or letter == “” or \
Letter == “” or letter == “” or \
Letter == “” :
Print(“”)
Elif letter == “y” :
Print(“Sometimes it’a sa vowel.. sometime ita consonant”)
Else
Print(“it’s a consonant”)
OUTPUT

2: Making a random dice using random


from random import randrange,randint #time is module
def():
for i in range(1,4):
value = randrange(1,7)
print("")
if value == 1 :
print("")
print("")
print("")
elif value == 2 :
print("")
print("")
print( “")
elif value == 3 :
print("")
print("")
print("")

9
Programming Fundamentals (CS-116L) SSUET/QR/114

elif value == 4 :
print("")
print("")
print("")
elif value == 5 :
print("")
print("")
print("")
elif value == 6 :
print("")
print("")
print("")
else :
print("")
print("")
()
OUTPUT

3: Ternary Operator
x=3
answer = ‘positive’ if x>0 else ‘negative’
print(‘answer’)
x = -3
answer = “positive” if x>0 else “ negative”
print(“Answer”)
OUTPUT

10
Programming Fundamentals (CS-116L) SSUET/QR/114

4: Importing from Random


From random import randidt, randrange,seed
Seed()
For I in range ():
Value=randit()
X=randrange()
Print(“value---->”,value)
Print(“x--->”,x)
OUTPUT:

5: Importing from time


From time import perf_counter()
While(true)
Print(“Enter your name”)

11
Programming Fundamentals (CS-116L) SSUET/QR/114

Start_time=time=perf_counter)
Names=imput()
Elapesd=perf_counter()-start_time
If(Elapsed>3)
Print(“no tell me your original name”)
Else
Print(“yes I think you telling truth”)
Break
()
OUTPUT:

6: turtle code:
from turtle import Turtle
from random import randint
from time import perf_counter
def randomWalk(t,turns,distance=20):
"""turns a random number of degrees and moves a given distance for a fixed number of turns>"""
start_time=perf_counter()
for x in range(turns):
if x % 2==0:
t.left(randint(0.270))
else:
t.right(randint(0,270))
t.forward(distance)
elapsed=perf_counter()-start_time
print(f"Maximum seconds it takes to draw {elapsed}")
def main():
t= Turtle()
t.shape("turtle")
randomWalk(t,40,30)
main()
OUTPUT:

12
Programming Fundamentals (CS-116L) SSUET/QR/114

13

You might also like