WEEK 1011 -Simple Calculator Program in Python
WEEK 1011 -Simple Calculator Program in Python
Step 1: Prompting Input from the User, we shall accept input for two variables.
In this step, we shall take the user's input using Python's input() function. It is the same
as when we enter numbers in an accurate calculator to perform arithmetic operations. We
shall ask the user to input two variables using each variable's input() function.
Let's have the program perform the prompt for the two numbers:
Code:
number_1 = input('Please, Enter the first number: ')
number_2 = input('Please, Enter the second number: ')
Output:
Please, Enter the first number: 20
Please, Enter the second number: 10
Step2: Define and Add operators or functions such as add(), subtract(), multiply() and
divide() to estimate respective functions.
Code:
number_1 = int(input('Please, Enter the first number: '))
number_2 = int(input('Please, Enter the second number: '))
# arithmetic operation: Addition
print('{} + {} = '.format(number_1, number_2))
sum = int(number_1) + int(number_2)
print(“the sum is: ”, sum)
if operation == 1:
print(number_1, "+", number_2, "=",
add(number_1, number_2))
elif operation == 2:
print(number_1, "-", number_2, "=",
subtract(number_1, number_2))
elif operation == 3:
print(number_1, "*", number_2, "=",
multiply(number_1, number_2))
elif operation == 4:
print(number_1, "/", number_2, "=",
divide(number_1, number_2))
else:
print("Please enter Valid input")
Output:
Hi, I am a Calculator!
Please select which of the following arithmetic operations you want to perform-
1. Add
2. Subtract
3. Multiply
4. Divide
1, 2, 3 or 4 :3
Please, Enter the first number: 10
Please, Enter the second number: 20
10 * 20 = 200