Q1. Write a python program to show how to use else clause with try and except clauses.
def calculate_bmi(height, weight):
""" calculate body mass index (BMI) """
return weight / height**2
def evaluate_bmi(bmi):
""" evaluate the bmi """
if 18.5 <= bmi <= 24.9:
return 'healthy'
if bmi >= 25:
return 'overweight'
return 'underweight'
def slip2():
try:
= float(input('Enter your height (meters):'))
height = float(input('Enter your weight (kilograms):'))
weight
except ValueError as error:
print(error)
else:
= round(calculate_bmi(height, weight), 1)
bmi = evaluate_bmi(bmi)
evaluation
print(f'Your body mass index is {bmi}')
print(f'This is considered {evaluation}!')
slip2()
OUTPUT
Enter your height (meters):1.4
Enter your weight (kilograms):75
Your body mass index is 38.3
This is considered overweight!
Q2. Write a python program to count and display even and odd numbers of a List.
= [21,3,4,6,33,2,3,1,3,76]
list1 = 0, 0
even_count, odd_count # enhanced for loop
for num in list1:
#even numbers
if num % 2 == 0:
+= 1
even_count#odd numbers
else:
+= 1
odd_countprint("Even numbers available in the list: ", even_count)
print("Odd numbers available in the list: ", odd_count)
OUTPUT
Even numbers available in the list: 4
Odd numbers available in the list: 6
Q2. Write a python program to find sum of items of a Dictionary.
def Sum(dic):
sum=0
#iterate through values
for i in dic.values():
sum=sum+i
return sum
={ 'x':30, 'y':40, 'z':50 }
dicprint("Dictionary: ", dic)
print("sum: ",Sum(dic))
OUTPUT
Dictionary: {'x': 30, 'y': 40, 'z': 50}
sum: 120
0 Comments:
Post a Comment