Programming in Python – Assignment 2 (Using
Functions)
2.1 Voting Eligibility
def check_vote():
age = int(input("Enter age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
check_vote()
2.2 Divisible by 5
def divisible_by_5():
n = int(input("Enter an integer: "))
print("Divisible by 5" if n % 5 == 0 else "Not divisible by 5")
divisible_by_5()
2.3 Smallest of Two Numbers
def smallest_two():
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Smallest:", a if a < b else b)
smallest_two()
2.4 Positive / Negative / Zero
def check_number():
n = int(input("Enter number: "))
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")
check_number()
2.5 Biggest of Three Numbers
def biggest_three():
a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))
print("Biggest:", max(a, b, c))
biggest_three()
2.6 Age Category
def age_category():
age = int(input("Enter age: "))
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
else:
print("Adult")
age_category()
2.7 Leap Year
def leap_year():
y = int(input("Enter year: "))
if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0:
print("Leap Year")
else:
print("Not a Leap Year")
leap_year()
2.8 Divisible by 3 and/or 7
def divisible_3_7():
n = int(input("Enter integer: "))
if n % 3 == 0 and n % 7 == 0:
print("Divisible by both 3 and 7")
elif n % 3 == 0:
print("Divisible by only 3")
elif n % 7 == 0:
print("Divisible by only 7")
else:
print("Divisible by neither 3 nor 7")
divisible_3_7()
2.9 Grade Calculation
def grade():
m = float(input("Enter marks %: "))
if m < 35: g = "F"
elif m < 50: g = "D"
elif m < 60: g = "C"
elif m < 70: g = "B"
elif m < 80: g = "A"
elif m < 90: g = "E"
else: g = "O"
print("Grade:", g)
grade()
2.10 Income Tax
def income_tax():
income = float(input("Enter annual income: "))
if income <= 300000: tax = 0
elif income <= 700000: tax = income * 0.05
elif income <= 1000000: tax = income * 0.10
elif income <= 1200000: tax = income * 0.15
elif income <= 1500000: tax = income * 0.20
else: tax = income * 0.30
print("Tax Amount:", tax)
income_tax()
2.11 Electricity Bill
def electricity_bill():
prev = int(input("Enter previous reading: "))
pres = int(input("Enter present reading: "))
units = pres - prev
if units <= 50:
bill = units * 2.90
elif units <= 200:
bill = 50*2.90 + (units-50)*4.70
elif units <= 400:
bill = 50*2.90 + 150*4.70 + (units-200)*5.70
else:
bill = 50*2.90 + 150*4.70 + 200*5.70 + (units-400)*6.10
print("Units:", units)
print("Bill Amount:", bill)
electricity_bill()
2.13 Triangle Perimeter
def triangle_perimeter():
a,b,c = map(int,input("Enter three sides: ").split())
if a+b>c and a+c>b and b+c>a:
print("Perimeter:", a+b+c)
else:
print("Invalid Triangle")
triangle_perimeter()
2.14 Oldest of Five Persons
def oldest_person():
ages = [int(input(f"Enter age {i+1}: ")) for i in range(5)]
print("Oldest Age:", max(ages))
oldest_person()
2.15 Triangle Type
def triangle_type():
a,b,c = map(int,input("Enter three sides: ").split())
if a+b>c and a+c>b and b+c>a:
if a==b==c:
print("Equilateral")
elif a==b or b==c or a==c:
print("Isosceles")
else:
print("Scalene")
else:
print("Invalid Triangle")
triangle_type()
2.16 Days in Month
def days_in_month():
m = int(input("Enter month number: "))
if m in [1,3,5,7,8,10,12]:
print("31 days")
elif m in [4,6,9,11]:
print("30 days")
elif m == 2:
print("28 days")
else:
print("Invalid month")
days_in_month()
2.17 Profit or Loss %
def profit_loss():
cp = float(input("Enter cost price: "))
sp = float(input("Enter selling price: "))
if sp > cp:
print("Profit %:", ((sp-cp)/cp)*100)
else:
print("Loss %:", ((cp-sp)/cp)*100)
profit_loss()
2.18 Day Name
def day_name():
d = int(input("Enter day number: "))
days = ["MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY","SUNDAY"]
if 1 <= d <= 7:
print(days[d-1])
else:
print("Invalid")
day_name()
2.19 Simple Calculator
def calculator():
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Enter operator: ")
if op == '+': print(a+b)
elif op == '-': print(a-b)
elif op == '*': print(a*b)
elif op == '/': print(a/b)
else: print("Invalid operator")
calculator()
2.20 Age Calculation
def age_calc():
d1,m1,y1 = map(int,input("Enter DOB (dd mm yyyy): ").split())
d2,m2,y2 = map(int,input("Enter calc date (dd mm yyyy): ").split())
days1 = y1*365 + m1*30 + d1
days2 = y2*365 + m2*30 + d2
diff = days2 - days1
y = diff // 365
diff %= 365
m = diff // 30
d = diff % 30
print("Age:", y,"years",m,"months",d,"days")
age_calc()