Program:
def add(x,y):
return x+y
def sub(x,y):
return x-y
def mul(x,y):
return x*y
def div(x,y):
return x/y
def main(opt):
n1=int(input("Enter 1st Number:"))
n2=int(input("Enter 2nd Number:"))
#implementing switch case using Dictionary Mapping
switcher = {
1: add(n1,n2),
2: sub(n1,n2),
3: mul(n1,n2),
4: div(n1,n2)
}
return switcher.get(opt, "!!Invalid Option!!")
# Driver program
if __name__ == "__main__":
print("********Select operation*********")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
opt=int(input("\n\t-->>Enter Option: "))
print("\n-->>Result=",main(opt) )
Expected O/P:
********Select operation*********
1.Add
2.Subtract
3.Multiply
4.Divide
-->>Enter Option: 1
Enter 1st Number:10
Enter 2nd Number:21
-->>Result= 31
_______________________________________________
********Select operation*********
1.Add
2.Subtract
3.Multiply
4.Divide
-->>Enter Option: 1
Enter 1st Number:10
Enter 2nd Number:21
-->>Result= 31
______________________________________________
********Select operation*********
1.Add
2.Subtract
3.Multiply
4.Divide
-->>Enter Option: 5
Enter 1st Number:12
Enter 2nd Number:2
-->>Result= !!Invalid Option!!
4 Comments
This blog aware me about different programs which can become very useful for our friends and kids. Few websites provide combined courses and few of the are separately for single subject. Glad to get this information.
ReplyDeletePython Tutorial
I found your blog on Google and read a few of your other posts. I just added you to my Google News Reader. You can also visit Caching In Python for more Coding Dolphin related information and knowledge, Keep up the great work Look forward to reading more from you in the future.
ReplyDeleteI really appreciate your work which you have shared here about the CR123A Battery. The article you have shared here is very informative and the points you have mentioned are very helpful. Thank you so much.Best CR123A Battery
ReplyDeleteLearn Python Programming Course Basic to Advanced Level - Life is Short - you Need Python Course. Further More Details Here-+91-9310332343.
ReplyDeletePython training course in Delhi
Python training institute in Delhi
Post a Comment