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:
height = float(input('Enter your height (meters):'))
weight = float(input('Enter your weight (kilograms):'))
except ValueError as error:
print(error)
else:
bmi = round(calculate_bmi(height, weight), 1)
evaluation = evaluate_bmi(bmi)
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.
list1 = [21,3,4,6,33,2,3,1,3,76]
even_count, odd_count = 0, 0
# enhanced for loop
for num in list1:
#even numbers
if num % 2 == 0:
even_count+= 1
#odd numbers
else:
odd_count+= 1
print("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
dic={ 'x':30, 'y':40, 'z':50 }
print("Dictionary: ", dic)
print("sum: ",Sum(dic))OUTPUT
Dictionary: {'x': 30, 'y': 40, 'z': 50}
sum: 120
0 Comments:
Post a Comment