2
first,second,third = tokenize(s)
4
#print "%s is a number -> put in var1" % first
7
#print "%s is not a number -> eval" % first
8
left = evaluate(first[1:-1])
10
if is_operand(second):
11
#print "%s is an operation -> put in operation" % second
14
print "%s is not an operation, skipping (this will crash)" % second
17
#print "%s is a number -> put in var2" % third
20
#print "%s is not a number -> eval" % third
21
right = evaluate(third[1:-1])
23
result = do_op(left,operation,right)
24
#print "we return the value of %s %s %s which is %s" % (left,operation,right,result)
29
if s[0] not in ('(',')') :
30
first = read_number(s)
31
second = s[len(first)]
32
third = s[len(first)+1:]
47
#print "tokenization : %s, %s , %s" % (first,second,third)
48
return first,second,third
57
def do_op(left,operation,right):
59
# we may have None in one of those,
60
# for example when dividing by 0
62
if not (left and right) :
68
elif operation == "*" :
71
elif operation == "/" :
74
# we may only allow net divisions
78
elif operation == "-" :
85
return s in ["+","/","*","-"]
90
print evaluate(sys.argv[1])
92
if __name__ == "__main__":