Python Calculator With Two Float Numbers As Parameters
Write a menu driven program using functions to make a calculator having the following operations: Add, Subtract, Multiply and Divide. Define four functions, with two float numbers
Solution 1:
Hope this code may help you
defadd(a,b):
print(a+b)
defsubract(a,b):
print(a-b)
defmultipy(a,b):
print(a*b)
defdivide(a,b):
print(a/b)
ch="y"while ch=="y"or ch=="Y":
x = float(input("first number : "))
y = float(input("second number: "))
print(".....MENU.......\n 1.Add\n 2.Subtract\n 3.Multiply\n 4.Divide\n")
op=int(input("Enter your choice : "))
if op==1:
add(x,y)
elif op==2:
subract(x,y)
elif op==3:
multipy(x,y)
elif op==4:
divide(x,y)
else: print("invalid Choice")
ch=input("Do you want to continue?(Y/y) : ")
you may get output as:
first number : 10
second number: 20
.....MENU.......
1.Add
2.Subtract
3.Multiply
4.Divide
Enter your choice : 130.0Do you want tocontinue?(Y/y) : y
first number : 20.7
second number: 13.2
.....MENU.......
1.Add
2.Subtract
3.Multiply
4.Divide
Enter your choice : 27.5Do you want tocontinue?(Y/y) : y
first number : 3.6
second number: 7.9
.....MENU.......
1.Add
2.Subtract
3.Multiply
4.Divide
Enter your choice : 328.44Do you want tocontinue?(Y/y) : y
first number : 45
second number: 7
.....MENU.......
1.Add
2.Subtract
3.Multiply
4.Divide
Enter your choice : 46.428571428571429Do you want tocontinue?(Y/y) : n
Process finished withexit code 0
this is a simple basic problem ... First you have to try your own code then if you get any error while solving. You have to ask Don't just directly post your question..
Post a Comment for "Python Calculator With Two Float Numbers As Parameters"