Skip to content

A simple calculator using python that can add, subtract, multiply or divide two numbers depending upon the input from the user.

Notifications You must be signed in to change notification settings

Rahila-me/Simple-Calculator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Simple Calculator

A simple calculator using python that can add, subtract, multiply or divide numbers depending upon the input from the user.

#This function adds the two numbers def add(x, y): return (x + y)

#This function subtracts the two numbers def subtract(x, y): return (x - y)

#This function multiplies the two numbers def multiply(x, y): return (x * y)

#This function divides the two numbers def divide(x, y): return (x / y)

print("Select your operation -> ") print("Enter 1 for addition (+)") print("Enter 2 for subtraction (-)") print("Enter 3 for multiplication (*)") print("Enter 4 for division (/)")

while True: #Get the input from the user choice = input("Enter your choice (1, 2, 3 or 4): ")

 #To check if the choice is one of the four options
 if choice in ('1', '2', '3', '4'):
     num1 = float(input("Please enter the first number: "))
     num2 = float(input("Please enter the second number: "))

     if choice == '1':
         print(num1, " + ", num2, " = ", add(num1, num2))

     elif choice == '2':
         print(num1, " - ", num2, " = ", subtract(num1, num2))

     elif choice == '3':
         print(num1, " * ", num2, " = ", multiply(num1, num2))

     elif choice == '4':
         print(num1, " / ", num2, " = ", divide(num1, num2))
     break #To exit the while True loop

 #Show error if the choice is not one of the four options
 else:
     print("Invalid Input!")

About

A simple calculator using python that can add, subtract, multiply or divide two numbers depending upon the input from the user.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%