ungleich-learning-circle/youngjin.han/python-the-hard-way/calc.py

45 lines
1.2 KiB
Python

import sys
a_py_file, a_calc_file = sys.argv
def input_and_calculate_one_line():
l_input_array = list()
l_s_result = ""
l_input_string_num = 0
l_input_string = input("""Only numbers and spaces are allowed.
'q' is exit.
> """).split()
if "q" != l_input_string[0] or 1 != len(l_input_string):
try:
for i in l_input_string:
l_input_string_num += 1
l_input_array.append(int(i))
if l_input_string_num != len(l_input_string):
l_s_result += f"{i} + "
else:
l_s_result += f"{i} = "
l_result = sum(l_input_array)
l_s_result += f"{l_result}"
except:
l_s_result = f"input error : input string = {l_input_string}"
print(l_s_result)
return l_s_result
def editor(p_calc_file):
l_calc_result = open(p_calc_file, 'w')
l_calc_result.write("")
l_calc_result.close()
while True:
l_s_result = input_and_calculate_one_line()
if "" != l_s_result:
l_calc_result = open(p_calc_file, 'a')
l_calc_result.write(f"""{l_s_result}\n""")
l_calc_result.close()
else:
exit(0)
editor(a_calc_file)