+ 1

How to code a calculator

I keep trying but it isn't working. Please anyone help me https://sololearn.com/compiler-playground/cMJYvPV87vFL/?ref=app

25th Aug 2025, 7:32 AM
CodeVortex 🧙
CodeVortex 🧙 - avatar
6 Antworten
+ 5
@Ikechika Dickson You had problems with your indention and some syntax errors and also some incomplete exception handling... I have edited your code to the bare minimum and simple to understand, please note the indention. Now its working perfectly. Below is the code. #======== START OF CODE ======== first_number=int(input("Please enter first number: ")) print(first_number) second_number=int(input("Please enter second number: ")) print(second_number) operator = input("Choose an operation (+) or (-):") if(operator[0]=="+"): result = first_number + second_number elif(operator[0]=="-"): result = first_number - second_number print(operator[0]) print("Your answer is" , result) #======== END OF CODE ======== Let me know if you want me to add exception handling
25th Aug 2025, 8:12 AM
Mashtullah
Mashtullah - avatar
+ 4
The error message should be pretty clear - unexpected indent. Python uses indentation to distinguish structeres (e.g. where if starts and ends). Your indents are all messed up.
25th Aug 2025, 7:59 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
@CodeVortex I have redone the solution to be more complete with exception handling. https://www.sololearn.com/en/compiler-playground/c2vUxCe0092Y try and play with it there i have also tried to comment most of the lines so you can understand. Incase you have a question, let me know.
25th Aug 2025, 9:21 AM
Mashtullah
Mashtullah - avatar
+ 1
1. your indentations are all wrong. 2.you can't concatenate strings and numbers 3.you have to assign the '+' or '-' operation to a variable 4.your conditions do not need to be in parenthesis, and the syntax is wrong for if(+). print("please enter two numbers") try: first_number=int(input()) second_number=int(input()) print(first_number, second_number) except ValueError: print('please enter a valid number') print("choose an operation (+) or (-)") try: operation = input() print(operation) if(operation == "+"): result = first_number + second_number elif(operation == "-"): result = first_number - second_number else: print('please enter + or -') print(first_number, operation, second_number, "=", result) except EOFError: print('please enter a valid operation')
25th Aug 2025, 11:06 AM
Bob_Li
Bob_Li - avatar
0
Can you code one and show me 🥲
25th Aug 2025, 8:00 AM
CodeVortex 🧙
CodeVortex 🧙 - avatar
0
mashtullah Please explain better
25th Aug 2025, 8:25 AM
CodeVortex 🧙
CodeVortex 🧙 - avatar