Skip to content Skip to sidebar Skip to footer

Python 2.7 Indentation Error

Hi I am new to Python having an indentation error in the following code: print '------------------------- Basics Arithamatic Calculator-------------------------' int_num_one=inpu

Solution 1:

It looks like the code 'inside' the for loop was not indented correctly, this should work.

for char_symbol in list_options:
    print"Char symbol is %r" % (char_symbol)
    bool_operation_Not_Found=True
    if char_symbol==char_selected_option:
        int_result=str(int_num_one) + char_selected_option + str(int_num_two)
        print int_result
        print"%d %s %d = %d" % (int_num_one, char_selected_option, int_num_two, eval(int_result))
        bool_operation_Not_Found=False
        breakif bool_operation_Not_Found:
    print"Invalid Input"

Solution 2:

This could be a lot shorter; instead of using a set and eval, you could use a dictionary of functions:

ops = {'+': lambda a, b: a + b,
       '-': lambda a, b: a - b,
       '/': lambda a, b: float(a) / b,
       '*': lambda a, b: a * b,
       }
try:
    print"%d %s %d = %f" % (
            int_num_one,
            char_symbol,
            int_num_two,
            ops[char_symbol](int_num_one, int_num_two),
            )
except KeyError:
    print"Invalid Input"

Post a Comment for "Python 2.7 Indentation Error"