Answer to Question #181887 in Python for kavya

Question #181887

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.


1
Expert's answer
2021-04-15T16:26:53-0400
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')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS