IF-ELIF-ELSE STRUCTURE IN PYTHON
Study these examples carefully and try to write the same of your own.
Write a program in python to take ‘age’ of the user as input and check the person is adult or
child.
def ageCheck(): //Function definition
age=input("Enter your age:")
a=int(age)
if a>=18:
print("You are adult")
else:
print("You are a child”)
To call the function:
1) Run the module
Python shell will appear.
Write:
Import filename (.py file where you have saved your function)
For ex, if you have saved the function ageCheck() in age.py file then write:
Import age
age.ageCheck(); //A drop down box will appear whenever you type the dot where you
can find your function name.
2) Make a .py file where you can write the code-
Import filename (.py file where you have saved your function)
For ex, if you have saved the function ageCheck() in age.py file then write:
Import age
age.ageCheck(); //A drop down box will appear whenever you type the dot where you
can find your function name.
Now, run this module.
Output will be shown in python shell,[& age.py- GAPython progs\agery B74) - ao x
File Edit Format Run Options Window Help
08
(age)
ar ap=18:
PD Type here to search fe}[B ngecheckcapy- Python proge\ageCheckCallpy 74) =
fle Eat Formst Ruy Options Window Helo
2. Program to check that a given number is odd or even,
def odd_even(number):
if number==0:
print("Nutural number")
elif number%:
print("Even number")
else:
print("Odd number")
Call the function in a way mentioned above.
3. Program to take input the total marks and obtained marks of a student form key board.
Calculate the percentage of marks. If marks is greater than 60% then print GRADE A.If the marks is greater than 50% then print GRADE B. If the marks is greater than 30% then
print GRADE C. Other wise print FAILED.
def calculate_percentage():
total_marks=input("Enter Total Marks ")
obtained_marks-input("Enter obtained Marks ")
‘tzint(total_marks)
o-int(obtained_marks)
percentage=0/t*100
if percentage>=60:
print("Grade A")
elif percentage >=50:
print("Grade 8")
elif percentage >= 30:
print("Grade C")
else:
print(*Failed")
4, Write a menu driven program to implement a calculator using python.
def calculator():
print("Enter your Choice")
print("1.add")
print("2.subtract")
print("3.multiply")
print("4.divide")
choice=input()
choice=int(choice)
print(choice)
if choice==1:
print("Enter 1st number")
ainput()
aeint(a)
print("Enter 2nd number")
print("Result of addition"+str(c))
elif choice==2:
print("Enter 1st number")
ainput()
azint(a)
print("Enter 2nd number”)cab
print("Result of subtraction"str(c})
elif choice:
print("Enter 1st number")
azinput()
acint(a)
print("Enter 2nd number")
print("Result of multiplication"+atr(c))
else:
print("Enter 1st number")
asinput()
azint(a)
print("Enter 2nd number")
beinput()
beint{b)
c=a//b
print(’Result of division" +str(c})