0

Hello I am having a trouble while doing my simple calculator code :D

def cal():
while True:
 print ("welcome to my calculator!")
 print("choose an operation")
 op = input(" +, - ,/ ,*")
if op == "+":
 num1 = float(input("enter your first number:"))
 num2 = float(input("enter your second number:"))
 print(str(num1 + num2)
elif op == "/":
 num1 = float(input("enter your first number:"))
 num2 = float(input("enter your second number:"))
 print(str(num1 / num2)
else:
break 
cal()

When ever I run the code it says invalid syntax at the elif

what is wrong here?

asked Aug 4, 2017 at 19:16
3
  • It's hard to tell with your indentation being 2 spaces Commented Aug 4, 2017 at 19:17
  • Missing parentheses on the print statement before the elif Commented Aug 4, 2017 at 19:17
  • 2
    print(str(num1 + num2) has two left parentheses and one left parenthesis. This is a problem. There are other lines like this as well. Make sure your parentheses match. Commented Aug 4, 2017 at 19:17

2 Answers 2

4

You never closed the bracket on the print function. Same goes for the other if statement. You should use indentation of 4 spaces in the future, too.

if op == "+":
 num1 = float(input("enter your first number:"))
 num2 = float(input("enter your second number:"))
 print(str(num1 + num2))
answered Aug 4, 2017 at 19:18
Sign up to request clarification or add additional context in comments.

4 Comments

you can use 4 spaces, but i just find using tab more convinient.
@Stack I always try to make my code as clean as possible, which also involves following PEP8
It doesnt matter, tab gives the same number of spaces. It is just easy to press tab once than pressing space 4 times. Anyways follow what you want : )
@Stack Sublime allows me to indent using spaces, hitting tab puts 4 spaces in the code. But you're right, this is really a matter of preference. :)
3

You missed a bunch of brackets. If you want to take your program further use this as a model:

# This function adds two numbers 
def add(x, y):
 return x + y
# This function subtracts two numbers 
def subtract(x, y):
 return x - y
# This function multiplies two numbers
def multiply(x, y):
 return x * y
# This function divides two numbers
def divide(x, y):
 return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
# Take input from the user 
choice = input("Enter choice(1/2/3/4):")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice == '1':
 print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
 print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
 print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
 print(num1,"/",num2,"=", divide(num1,num2))
else:
 print("Invalid input")
answered Aug 4, 2017 at 19:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.