PRACTICE SHEET
CONDITIONAL STATEMENTS
Name- Isha Mahadev
RegistrationNo.-23BCE10163
Slot- D11+D12+D13
1. A school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 – A. Ask user to enter marks and print the corresponding grade.
Source Code:
marks = float(input("Enter marks: "))
if marks < 25:
grade = 'F'
elif marks < 45:
grade = 'E'
elif marks < 50:
grade = 'D'
elif marks < 60:
grade = 'C'
elif marks < 80:
grade = 'B'
else:
grade = 'A'
print(f"Grade: {grade}")
Sample Input and Output:
marks = float(input("Enter marks: "))
if marks < 25:
grade = 'F'
elif marks < 45:
grade = 'E'
elif marks < 50:
grade = 'D'
elif marks < 60:
grade = 'C'
elif marks < 80:
grade = 'B'
else:
grade = 'A'
print(f"Grade: {grade}")
Enter marks: 55
Grade: C
Screenshot of the Output:
Question No 2: Write a Python program that checks if a given year is a leap year or not.
A leap year is divisible by 4, except for years divisible by 100 but not divisible by 400.
Source Code:
yr = int(input("Enter a year: "))
is_yr = False
if yr % 4 == 0 and yr % 100 != 0:
is_yr = True
if yr % 400 == 0:
is_yr = False
if is_yr:
print(f"It is a leap year")
else:
print(f"It is not a Leap Year")
Sample Input and Output:
yr = int(input("Enter a year: "))
is_yr = False
if yr % 4 == 0 and yr % 100 != 0:
is_yr = True
if yr % 400 == 0:
is_yr = False
if is_yr:
print(f"It is a leap year")
else:
print(f"It is not a Leap Year")
Enter a year: 2015
It is not a Leap Year
Screenshot of the Output:
Question No 3: Accept three sides of a triangle and check whether the triangle is
possible or not. (Hint: Triangle is possible only when sum of any two sides is greater
than 3 rd side).
Source Code:
a = int(input("Enter 1st side: "))
b = int(input("Enter 2nd side: "))
c = int(input("Enter 3rd side: "))
A=a+b
B=b+c
C=c+a
if a < B and b < C and c < A:
print("Its a triangle!")
else:
print("Its not a triangle.")
Sample Input and Output:
a = int(input("Enter 1st side: "))
b = int(input("Enter 2nd side: "))
c = int(input("Enter 3rd side: "))
A=a+b
B=b+c
C=c+a
if a < B and b < C and c < A:
print("Its a triangle!")
else:
print("Its not a triangle.")
Enter 1st side: 3
Enter 2nd side: 4
Enter 3rd side: 5
Its a triangle!
Screenshot of the Output:
Question No 4: Write a Python program that converts temperatures between Celsius
and Fahrenheit. The user should input the temperature and its unit (C or F), and the
program should convert it to the other unit.
Source Code:
temp = int(input("Enter a temperature: "))
unit = str(input("Enter your unit(C/F): "))
y = temp
if unit == "C":
temp = (9 * y) / 5 + 32
print(f"Temperature in Farenheit is {temp} ")
elif unit == "F":
temp = 5*(y-32)/9
print(f"Temperature in Celsius is {temp}")
else:
print("Not a valid unit")
Sample Input and Output:
temp = int(input("Enter a temperature: "))
unit = str(input("Enter your unit(C/F): "))
y = temp
if unit == "C":
temp = (9 * y) / 5 + 32
print(f"Temperature in Farenheit is {temp} ")
elif unit == "F":
temp = 5*(y-32)/9
print(f"Temperature in Celsius is {temp}")
else:
print("Not a valid unit")
Enter a temperature: 37
Enter your unit(C/F): C
Temperature in Farenheit is 98.6
Screenshot of the Output:
Question No 5:
A student will not be allowed to sit in exam if his/her attendance is less than 75%. Take
following input from user -Number of classes held, Number of classes attended and
print percentage of class attended. Allow the student to sit for the exam if he/she has
medical case. Ask user if he/she has medical cause or not (‘Y’ or ‘N’) and print
accordingly. Finally Print whether the student is allowed to sit in exam or not.
Source Code:
attended = int(input("Enter no. of classes attended: "))
held = int(input("Enter total no. of classes held: "))
percentage = (attended/held)*100
medical = input("Do you have any medical cause? (Y/N): ")
if medical == "Y" or medical == "N":
pass
else:
print("Enter valid input")
if percentage > 75:
print("Allowed")
elif medical == "Y":
print("Allowed")
else:
print("Not Allowed")
Sample Input and Output:
attended = int(input("Enter no. of classes attended: "))
held = int(input("Enter total no. of classes held: "))
percentage = (attended/held)*100
medical = input("Do you have any medical cause? (Y/N): ")
if medical == "Y" or medical == "N":
pass
else:
print("Enter valid input")
if percentage > 75:
print("Allowed")
elif medical == "Y":
print("Allowed")
else:
print("Not Allowed")
Enter no. of classes attended: 40
Enter total no. of classes held: 45
Do you have any medical cause? (Y/N): Y
Allowed
Screenshot of the Output:
Question No 6: Create a Python program that calculates and categorizes a person’s Body
Mass Index (BMI) based on their height and weight.
Source Code:
h = float(input("Enter your height in m: "))
w = float(input("Enter your weight in kgs: "))
bmi = w/(h**2)
print(f"bmi is {bmi:.2f}")
if bmi < 18.5:
print("Underweight")
elif 18.5 <= bmi < 24.9:
print("Normal weight")
elif 25 <= bmi < 29.9:
print("Overweight")
else:
print("Obese")
Sample Input and Output:
h = float(input("Enter your height in m: "))
w = float(input("Enter your weight in kgs: "))
bmi = w/(h**2)
print(f"bmi is {bmi:.2f}")
if bmi < 18.5:
print("Underweight")
elif 18.5 <= bmi < 24.9:
print("Normal weight")
elif 25 <= bmi < 29.9:
print("Overweight")
else:
print("Obese")
Enter your height in m: 1.70
Enter your weight in kgs: 74
bmi is 25.61
Overweight
Screenshot of the Output:
Question No 7:
Create a Python program for a movie theatre that calculates ticket prices based on age
and time of day. Tickets for children (age < 12) are $5, adults (age >= 12) are $10, and
seniors (age >= 60) are $7. For evening shows (after 5 PM), there’s an additional $2
surcharge.
Source Code:
price = 0
age = int(input("Enter your age: "))
x = input("Is is an evening show(after 5 pm)? (Y/N): ")
if age < 12:
price = 5
elif age>= 12:
price = 10
elif age >= 60:
price = 7
if x == "Y":
price += 2
print(f"Your ticket is worth {price}$")
Sample Input and Output:
price = 0
age = int(input("Enter your age: "))
x = input("Is is an evening show(after 5 pm)? (Y/N): ")
if age < 12:
price = 5
elif age>= 12:
price = 10
elif age >= 60:
price = 7
if x == "Y":
price += 2
print(f"Your ticket is worth {price}$")
Enter your age: 18
Is is an evening show(after 5 pm)? (Y/N): Y
Your ticket is worth 12$
Screenshot of the Output:
Question No 8:
A company decided to give bonus of 10% to employee if his/her year of service is more
than 3 years. Ask user for their salary and year of service and print the net bonus
amount.
Source Code:
salary = int(input("What is your current salary? "))
yrs = int(input("How long have you worked with us? "))
z = salary * 10/100
if yrs > 3:
salary += z
print(f"Your bonus is {z} and final salary is {salary}")
Sample Input and Output:
salary = int(input("What is your current salary? "))
yrs = int(input("How long have you worked with us? "))
z = salary * 10/100
if yrs > 3:
salary += z
print(f"Your bonus is {z} and final salary is {salary}")
What is your current salary? 30000
How long have you worked with us? 5
Your bonus is 3000.0 and final salary is 33000.0
Screenshot of the Output:
Question No 9:
Ask user to enter age, gender (M or F), marital status (Y or N) and then using following
rules print their place of service. If employee is female, then she will work only in urban
area.If employee is a male and age is in between 20 to 40 then he may work in
anywhere. If employee is male and age is in between 40 to 60 then he will work in
urban areas only and any other input of age should print "ERROR".
Source Code:
age = int(input("Enter your age: "))
gen = input("Enter your gender(M/F): ")
if gen == "M" and 20<=age< 40:
print("You may work anywhere")
elif gen == "M" and 40<= age<= 60:
print("You may work in urban areas only")
elif gen == "F":
print("You may work in urban areas only")
else:
print("ERROR")
Sample Input and Output:
age = int(input("Enter your age: "))
gen = input("Enter your gender(M/F): ")
if gen == "M" and 20<=age< 40:
print("You may work anywhere")
elif gen == "M" and 40<= age<= 60:
print("You may work in urban areas only")
elif gen == "F":
print("You may work in urban areas only")
else:
print("ERROR")
Enter your age: 30
Enter your gender(M/F): M
You may work anywhere
Screenshot of the Output:
Question No 10:
A 4 digit number is entered through keyboard. Write a program to print a
new number with digits reversed as of orignal one.
E.g.-
INPUT: 1234 OUTPUT: 4321
INPUT: 5982 OUTPUT: 2895
Source Code:
rev = 0
while(no > 0):
a = no % 10
rev = rev * 10 + a
no = no // 10
print(rev)
Sample Input and Output:
rev = 0
while(no > 0):
a = no % 10
rev = rev * 10 + a
no = no // 10
print(rev)
Enter a no: 1234 Enter a no:5982
4321 2895
Screenshot of the Output:
11. What will be the output of the following?
if i < j:
if j < k:
i=j
else:
j=k
else:
if j < k:
j=i
else:
i=k
print(i,j,k)
(a) i = 3, j = 5, k = 7
(b) i = -2, j = -5, k = 9
(c) i = 8, j = 15, k = 12
(d) i = 13, j = 15, k = 13
(e) i = 3, j = 5, k = 17
(f) i = 25, j = 15, k = 17
Source Code:
i = 13
j = 15
k = 13
if i < j:
if j < k:
i=j
else:
j=k
else:
if j < k:
j=i
else:
i=k
print(i, j, k)
Sample Input and Output:
i = 13
j = 15
k = 13
if i < j:
if j < k:
i=j
else:
j=k
else:
if j < k:
j=i
else:
i=k
print(i, j, k)
output 13,13,13
Screenshot of the Output:
Question No 12:
Write a Python program that takes a user's input for their exam score and
provides the corresponding grade along with remarks. Consider the following
grading scale:
A: 90-100 (Perfect score marked as 'A' with the remark 'Perfect score!')
B: 80-89 (Remark: 'Good job!')
C: 70-79 (Remark: 'Average performance.')
D: 60-69 (Remark: 'Below average.')
F: Below 60 (Remark: 'Failed.').
Include additional remarks for a perfect score (100) and excellent performance
(grades A and B). Make use of if, elif, and else statements, as well as nested if
and else statements in the same program.
Source Code:
score = int(input("Enter your score: "))
if score >= 90 and score <= 100:
grade = 'A'
remarks = 'Perfect score!'
if score == 100:
Sample Input and Output:
score = int(input("Enter your score: "))
if score >= 90 and score <= 100:
grade = 'A'
remarks = 'Perfect score!'
if score == 100:
Enter your score: 65
Grade: D
Remarks: Below average.
Screenshot of the Output: