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

Calculator in python

So I am 'trying' to write a calculator in python and I am getting an indentation error. The code is at this address (I'm sorry it's really long)

http://pythontutor.com/visualize.html#code=equ+%3D+raw_input('Enter+the+problem%3A+')%0Aequ+%3D+equ.split('+')%0A%0Adef+error()%3A%0A%09print+'Please+enter+a+problem+with+the+following+format'%0A%09print+'number+operator+number'%0A%0Adef+convert()%3A%0A%09global+equ%0A%09if+str.isdigit(equ%5B0%5D)%3A%0A%09%09equ%5B0%5D+%3D+float(equ%5B0%5D)%0A%09if+str.isdigit(equ%5B2%5D)%3A%0A%09%09equ%5B2%5D+%3D+float(equ%5B2%5D)%0A%09else%3A%0A%09%09error()%0A%0Adef+solve(a,+b,+c)%3A%0A%09error_flag+%3D+'+'%0A%09if+b+%3D%3D+'%2B'%3A%0A%09%09answer+%3D+a+%2B+c%0A%09elif+b+%3D%3D+'-'%3A%0A%09%09answer+%3D+a+-+c%0A%09elif+b+%3D%3D+'*'%3A%0A%09%09answer+%3D+a+*+c%0A%09elif+b+%3D%3D+'/'%3A%0A%09%09answer+%3D+a+/+c%0A%09else%3A%0A++++++++print+'an+error'%0A++++print+'always+print+this'%0Aconvert()%0Asolve(equ%5B0%5D,+equ%5B1%5D,+equ%5B2%5D)%0A%0A%0A&mode=edit&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=2&rawInputLstJSON=%5B%5D

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi,

You're currently receiving an indentation error because you have mixed tabs and spaces which don't work together in Python as you're required to choose one or the other, I've reformatted your function which worked after I updated the code in the link provided.

def solve(a, b, c):
    error_flag = ' '

    if b == '+':
        answer = a + c
    elif b == '-':
        answer = a - c
    elif b == '*':
        answer = a * c
    elif b == '/':
        answer = a / c
    else:
        print 'an error'

    print 'always print this'