Python 2.7缩进错误

时间:2013-11-20 08:08:04

标签: python-2.7

您好我是Python新手,在以下代码中出现缩进错误:

print "------------------------- Basics Arithamatic Calculator-------------------------"

int_num_one=input('Enter First Num: ')
int_num_two=input('Enter Second Num: ')

list_options={"+", "-", "/", "*"}

print "Which operation you want to perform *, -, +, / ?"
char_selected_option=raw_input()
print "Operation selected is %r" % (char_selected_option)

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
break
if bool_operation_Not_Found:
print "Invalid Input"

2 个答案:

答案 0 :(得分:1)

看起来for循环内部的代码没有正确缩进,这应该可行。

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
        break
if bool_operation_Not_Found:
    print "Invalid Input"

答案 1 :(得分:0)

这可能会短得多;而不是使用集合和eval,您可以使用函数字典:

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"