0

Can anyone help me with this code?

So I wonder, why dosen't this code work on SoloLearn's code playground... num1, operator, num2 = input('Enter your calculation: ').split() print(operator) num1 = int(num1) num2 = int(num2) if operator == "+": print("{} + {} = {}".format(num1, num2, num1 + num2)) elif operator == "-" : print("{} - {} = {}".format(num1, num2, num1 - num2)) elif operator == "*" : print("{} * {} = {}".format(num1, num2, num1 *num2)) elif operator == "/" : print("{} / {} = {}".format(num1, num2, num1 / num2)) elif operator == "%" : print("{} - {} = {}".format(num1, num2, num1 % num2)) elif operator == "**" : print("{} - {} = {}".format(num1, num2, num1 ** num2)) else: print('Please use + - * / % or **')

23rd Feb 2017, 8:15 AM
Mask
Mask - avatar
4 Answers
+ 2
Maybe because your indentation is off in your if-elif-else? num1, operator, num2 = input('Enter your calculation: ').split() print(operator) num1 = int(num1) num2 = int(num2) if operator == "+": print("{} + {} = {}".format(num1, num2, num1 + num2)) elif operator == "-" : print("{} - {} = {}".format(num1, num2, num1 - num2)) elif operator == "*" : print("{} * {} = {}".format(num1, num2, num1 *num2)) elif operator == "/" : print("{} / {} = {}".format(num1, num2, num1 / num2)) elif operator == "%" : print("{} - {} = {}".format(num1, num2, num1 % num2)) elif operator == "**" : print("{} - {} = {}".format(num1, num2, num1 ** num2)) else: print('Please use + - * / % or **')
23rd Feb 2017, 9:25 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
@Mask wrote: "I found out the answer, I accidentally didnt leave a space :p" If you had read @ChaoticDawg answer, he told exactly that ^^
24th Feb 2017, 11:57 PM
visph
visph - avatar
0
I tried below code in code playground and it worked. num1,operator,num2 = input("Enter mathematical expression:").split() print(operator) num1=int(num1) num2=int(num2) if operator == '+': print("%s + %s = %d" % (str(num1),str(num2),num1+num2)) elif operator == "-": print("%s - %s = %d" % (str(num1),str(num2),num1-num2)) else: print('nothing other then + or -') print('Have a good day') What error are you getting?
23rd Feb 2017, 9:12 AM
Parminder Singh
- 1
I found out the answer, I accidentally didnt leave a space :p
24th Feb 2017, 1:26 PM
Mask
Mask - avatar