Learning Circle : python #7 - calc for sum
This commit is contained in:
parent
0b9adfd8f3
commit
821cfd55cd
1 changed files with 45 additions and 0 deletions
45
youngjin.han/python-the-hard-way/calc.py
Normal file
45
youngjin.han/python-the-hard-way/calc.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
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("Only numbers and spaces")
|
||||
|
||||
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)
|
Loading…
Reference in a new issue