In: Computer Science
PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide using functions. It needs to ask for the two numbers from the user and will ask the user for their choice of arithmetic operation
1- subtract
2- add
3- divide
4- multiply
# function to addition
def add(num1, num2):
return num1 + num2
# function for suntraction
def sub(num1, num2):
return num1 - num2
# function for multiplication
def multiply(num1, num2):
return num1* num2
# function for division
def divide( num1, num2):
return num1/num2
# main funciton
def main():
# variable for two numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# printing the menu
print("1-subtract\n2-add\n3-divide\n4-multiply\n")
# taking user choice
choice = int(input("Enter you choice: "))
# checking the user choice and calling the appropriate
function
if choice == 1:
print(sub(num1, num2))
elif choice == 2:
print(add(num1,num2))
elif choice == 3:
print(divide(num1, num2))
elif choice == 4:
print(multiply(num1,num2))
else:
print("Invalid Option..:(")
if __name__ == '__main__':
# calling main function
main()