Question: Input weight and height and print health category.
def health_category(weight, height):
bmi = weight / (height ** 2)
if bmi < 18.5:
return "Underweight"
elif 18.5 <= bmi < 24.9:
return "Normal weight"
elif 25 <= bmi < 29.9:
return "Overweight"
else:
return "Obese"
weight = float(input("Enter your weight in kg: "))
height = float(input("Enter your height in meters: "))
category = health_category(weight, height)
print(f"Your health category is: {category}")
output
Enter your weight in kg: 87
Enter your height in meters: 1.5
Your health category is: Obese
Question: Input units and calculate bill using tiered rates.
def calculate_bill(units):
if units <= 100:
bill = units * 0.50
elif 101 <= units <= 200:
bill = (100 * 0.50) + ((units - 100) * 0.75)
elif 201 <= units <= 300:
bill = (100 * 0.50) + (100 * 0.75) + ((units - 200) * 1.00)
else:
bill = (100 * 0.50) + (100 * 0.75) + (100 * 1.00) + ((units - 300) * 1.50)
return bill
units = float(input("Enter the number of units: "))
total_bill = calculate_bill(units)
print(f"Your total bill is:₹{total_bill:.2f}")
output
Enter the number of units: 256
Your total bill is:₹181.00
stored_username = "user123"
stored_password = "password123"
def verify_login(username, password):
if username == stored_username and password == stored_password:
return True
else:
return False
Question: Take username and password input and verify against stored values.
username_input = input("Enter your username: ")
password_input = input("Enter your password: ")
if verify_login(username_input, password_input):
print("Login successful!")
else:
print("Invalid username or password. Please try again.")
Enter your username: wer
Enter your password: wr
Invalid username or password. Please try again.
Question: Input three numbers and find the smallest using nested if.
def find_smallest(n1,n2,n3):
if n1<=n2:
if n1<+n3:
return n1
else:
return n3
else:
if n2<=n3:
return n2
else:
return n3
n1=float(input("Enter n1:"))
n2=float(input("Enter n2:"))
n3=float(input("Enter n3:"))
smallest = find_smallest(n1,n2,n3)
print(f"The smallest number is:{smallest}")
output
Enter n1:34
Enter n2:4
Enter n3:54
The smallest number is:4.0
Question: Input amount and apply discount based on slabs.
def apply_discount(amount):
if amount<=100:
discount=0
elif 101 <= amount <=500:
discount= 10
elif 501 <= amount <=1000:
discount= 15
else:
discount= 20
discount_amount = (discount/100)*amount
final_amount = amount - discount_amount
return discount, discount_amount,final_amount
amount = float (input("Enter the amount "))
discount, discount_amount, final_amount = apply_discount(amount)
print(f"original amount: {amount}")
print(f"discount applied: {discount}%")
print(f"discount amount:{discount_amount:.2f}")
print(f"final amount after discount: {final_amount:.2f}")
output
Enter the amount 65
original amount: 65.0
discount applied: 0%
discount amount:0.00
final amount after discount: 65.00
Question: Input a character and check if it is uppercase, lowercase, digit, or special character.
def check_character_type(char):
if [Link]():
return"Upper case"
elif [Link]():
return"Lower Case"
elif [Link]():
return "digit"
else:
return "Special Character"
char=input("enter the Character:")
result = check_character_type(char)
print(f"The Character'{char}' is a {result}.")
output
enter the Character:U
The Character'U' is a Upper case.
Question: Input marks and assign grades:
A: ≥90
B: 80–89
C: 70–79
D: 60–69
F: <60
Grade = int(input("Enter the Grade:"))
if Grade >= 90:
print("Grade A")
elif 80 <= Grade <= 89:
print("Grade B")
elif 70 <= Grade <= 79:
print("Grade C")
elif 60 <= Grade <= 69:
print("Grade D")
else:
print("Grade E")
output
Enter the Grade:77
Grade C