Reputation: 6223
Hi I am new to Python having an indentation error in the following code:
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"
Upvotes: 0
Views: 1072
Reputation: 2561
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"
Upvotes: 0
Reputation: 52
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
break
if bool_operation_Not_Found:
print "Invalid Input"
Upvotes: 2