0% found this document useful (0 votes)
12 views7 pages

PF lab 4

The document outlines a lab exercise focused on understanding conditional statements in Python for decision making. It includes identifying errors in provided code snippets, predicting outputs for certain programs, and writing new Python programs based on specified conditions. The exercises cover topics such as variable definitions, syntax errors, and implementing if-elif-else chains.

Uploaded by

mujtabaaliali888
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)
12 views7 pages

PF lab 4

The document outlines a lab exercise focused on understanding conditional statements in Python for decision making. It includes identifying errors in provided code snippets, predicting outputs for certain programs, and writing new Python programs based on specified conditions. The exercises cover topics such as variable definitions, syntax errors, and implementing if-elif-else chains.

Uploaded by

mujtabaaliali888
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/ 7

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, in the following Python programs.

1. CODE:

a = 500,b,c;
if ( a >= 400 ) :
b = 300
c = 200
print( “Value is:” , b, c )

ERROR: This is a NameError as ‘b’ and ‘c’ are not defined.


CORRECTION:

2. CODE:
&number = eval(input("Enter an integer: "))
print(type(number))
if number % 5 == 0:
print("HiFive")
else:
print("No answer")
ERROR: There are two noticable SyntaxErrors in this code:
I. A variable’s name cannot start with special characters like ‘&’.
II. There is a missing colon(:) at the end of the if statement .
CORRECTION:

3. CODE:
if score >= 60.0
grade = 'D'
elif score >= 70.0
grade = 'C'
if score >= 80.0
grade = 'B'
if score = >= 90,0
grade = 'A'
else:
grade = 'F'
ERROR: This is a SyntaxError as the if statements are missing a colon(:) at the end.
CORRECTION:There are two errors in this code:
I. The first error is a SyntaxError as the if statements are missing a colon(:) at the end.
II. The second error is a NameError as the name ‘score’ is not defined.
CORRECTION:

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


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

2. CODE:

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

3. CODE:

OUTPUT:
age = 15
if age < 4:
price = 0
elif age < 18:
price = 1500
else:
price = 2000
print("Your admission cost is Rs " + str(price) + ".")
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.

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
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 133 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.

You might also like