Python Exercises - How to build a basic calculator | Method #1 | Python Exercise #2

Pybeginner
By -
0


Here, we will create a Calculator in Python using if else as decision conditions.

It will be a good exercise to get us started with Python in practice.




Open your text editor and create a new Python file.


The calculator we are going to create will perform only 4 operations, which will be addition, subtraction, multiplication and division.


Code



#Menu
Print("Enter:\n\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n")  
 
# Menu
print("Enter:\n\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n")  
 
choice = input("Enter choice: ")  

a = int(input("Enter the first number: "))  
b = int(input("Enter the second number: "))  
 
if choice == '1':  
   print(a + b)  
 
elif choice == '2':  
   print(a - b)  
 
elif choice == '3':  
   print(a * b)
   
elif choice == '4':  
   print(a / b)  
   
else:  
   print("Invalid choice")  

Post a Comment

0Comments

Post a Comment (0)