Calculator in python

Asking the user which operation they want to perform

operator = input(“”"Please enter the operation that you want to perform:

  • , - , * , / , // , ** . “”")

We will introduce a varible skip which allows us to know wether the user wants to perform ‘**’

Orginally we set it to false, if the user wants to perform ‘**’ it turns true due to the if statement.

skip = False

if operator == ‘**’:
y = int(input(“Please enter your base number”))
z = int(input(“Please enter your exponent”))
r = y ** z
print(f"Your answer is: {r}")
skip = True

if not skip:

We define a list ‘calculation’ in which we will store all the input data.

Using the split function we will seperate each number.

calculation = input(“”"Please enter the numbers on which you want to perform the calculations.
Give a space after every number.
For example: 1 2 3 4 5…

“”“).split(” “)
print(” ")

Converting all str() to int()

calculation = [int(num) for num in calculation]

x = calculation

Addition

if operator == ‘+’:
result_x = sum(x)
print(f"Your answer is {result_x}")

Multiplication

elif operator == ‘*’:
result_x = 1
for no in x:
result_x *= no
print(f"Your answer is {result_x}")

Substraction

elif operator == ‘-’:
result_x = x[0]
for no in x[1:]:
result_x -= no
print(f"Your answer is {result_x}")

Division

elif operator == ‘/’:
result_x = x[0]
for no in x[1:]:
result_x /= no
print(f"Your answer is {result_x}")

Floor Division

elif operator == ‘//’:
result_x = x[0]
for no in x[1:]:
result_x //= no
print(f"Your answer is {result_x}")

Mod

elif operator == ‘%’:
result_x = x[0]
for no in x[1:]:
result_x %= no
print(f"Your answer is {result_x}")