Simple Calculator using functions

A code to add, subtract, multiply and divide multiple numbers at once using functions.

Welcome to the code! You will be able to add, subtract, multiply, and divide multiple numbers at once using functions. This code works just like a calculator to perform basic operations.

In this code, we have different functions to perform different arithmetic operations and we could perform operations on multiple numbers using lists.

Note: Loved the post? You too can publish your article on “Python for fun” which will be loved by millions. Contribute an article!

							
							
					repeat = "yes"
         
def add(length):
    lst=[]
    total = 0
    for n in range(length):
        num = float(input("Please enter the number to add."))
        lst.append(num)
        total+=num
    print("Total = ",total)
        
def sub(length):
    lst=[]
    
    for n in range(length):
        num = float(input("Please enter the numbers to subtract."))
        lst.append(num)
    total = lst[0] - sum(lst[1:])
    print("Total = ",total)

def mul(length):
    lst=[]
    total = 1
    for n in range(length):
        num = float(input("Please enter the numbers to multiply."))
        lst.append(num)
        total*=num 
    print("Total = ",total)

def div(length):
    lst=[]
    
    for n in range(length):
        num = float(input("Please enter the numbers to divide."))
        lst.append(num)
        total = 0
        for x in lst:
            if (total == 0):
                total = x
            else:
                total = total/int(x)
    print("Total = ",total)       
        
        
while repeat == "yes" or repeat == "y": 
    print("----------MENU----------")
    print('''Choose an operation: 
              1. Addition
              2. Subtraction
              3. Multiplication
              4. Division''')

    choice = int(input("Choose 1/2/3/4----"))
    if choice == 1:
        length = int(input("How many numbers do you want to add?"))
        add(length)
    
    elif choice == 2:
        length = int(input("How many numbers do you want to subtract?"))
        sub(length)

    elif choice == 3:
        length = int(input("How many numbers do you want to multiply?"))
        mul(length)
        
    elif choice == 4:
        length = int(input("How many numbers do you want to divide?"))
        div(length)

    else:
        print("Wrong Chocie!")
        
    repeat = input("Do you want to choose again?")
    repeat = repeat.lower()
    
print("See You Soon!")				
			
0 0 votes
Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments