Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

What's wrong with this code?

Hi!

I have written a really simple console calculator in Python, but it shows me some kind of an error, can you tell me what's wrong with that?

To see an error, scroll to the bottom.

That's my code:

def calc():
    operation = raw_input('''
    Hi! That's the calculator written in Python!
    If you want me to calculate something for you
    then choose the operator:
    + for addition
    - for substracting
    / for division
    * for multiplication
    ''')

    number_1 = int(input("Enter the first number here: "))
    number_2 = int(input("Enter the second number here: "))

    #Addition
    add = number_1 + number_2
    if operation == str("+"):
        print("{} + {} = {} ".format(number_1, number_2, add))

    #Substraction
    sub = number_1 - number_2
    elif operation == str("-"):
        print("{} - {} = {}".format(number_1, number_2, sub))

    #Division
    div = number_1 / number_2
    elif operation == str("/"):
        print("{} - {} = {}".format(number_1, number_2, div))

    #Multiplication
    mul = number_1 * number_2
    elif operation == str("*"):
        print("{} * {} = {}".format(number_1, number_2, mul))

    else:
        print("Give me a number not a string!")

    calc()

It shows me that there is a SyntaxError (log):

File "calculator.py", line 22 elif operation == str("-"): ^ SyntaxError: invalid syntax

2 Answers

Your if elif gets interrupted because there is code before the elif (add, sub, div, mul)

if condition:
     do something
elif condition:
     do something
else:
     do something

You break this statement with your add/div/mul variables so its not possible to do a elif/else

I dont really get what you want to do tbh, but maybe you know what to do now :)

I agree with Tobias; the if clause is interrupted.

Something like:

    if operation == str("+"):
        add = number_1 + number_2
        print("{} + {} = {} ".format(number_1, number_2, add))

    #Substraction
    elif operation == str("-"):
        sub = number_1 - number_2
        print("{} - {} = {}".format(number_1, number_2, sub))

... might work better for you; keep the operation within its own elif block?

Stev.e

or calculate them all then do the if blocks to manage the output:

add = number_1 + number_2
sub = number_1 - number_2
mul = number_1 * number_2
div = number_1 / number_2

if operation == str("+"):
  print("{} + {} = {} ".format(number_1, number_2, add))
elif operation == str("*"):
  print("{} * {} = {} ".format(number_1, number_2, mul))
.
.

Is there a switch/select/case statement in Python? That would be more elegant.

Something like that might work.