The input will be a single line containing two integers and operator(+, -, *, /, and %) similar to 3 + 5.
output
If the given operator is "+", print the sum of two numbers.
If the given operator is "-", print the result of the subtraction of the two numbers.
If the given operator is "*", print the multiplication of the two numbers.
If the given operator is "/", print the result of the division of the two numbers.
If the given operator is "%", print the result of the modulus operation of the two numbers.
s = input() # a single line containing two integers and operator
operator = '' # variable for operator
# variables for given numbers
num1 = ''
num2 = ''
for i in range(len(s)):
if not s[i].isdigit():
operator = s[i]
num1 = s[0:i] # the number to the left of the operator
num2 = s[i + 1: len(s)] # the number to the right of the operator
if not (num1.isdigit() and num2.isdigit()): # checking the correctness of the input
print('Invalid input')
else:
if operator == '+':
print(int(num1) + int(num2))
elif operator == '-':
print(int(num1) - int(num2))
elif operator == '*':
print(int(num1) * int(num2))
elif operator == '/':
print(int(num1) / int(num2))
elif operator == '%':
print(int(num1) % int(num2))
else:
print('Invalid input')
Comments
Leave a comment