From baeb4f858d78eda57e5f08812049585d24614f8f Mon Sep 17 00:00:00 2001 From: kjg Date: Mon, 1 Jun 2020 22:20:55 +0900 Subject: [PATCH] [Python applying learnings from 1..6] create calc.py --- kjg/python-the-hard-way/calc.py | 35 +++++++++++++++++++++++++++++++++ kjg/python-the-hard-way/sum.txt | 3 +++ 2 files changed, 38 insertions(+) create mode 100644 kjg/python-the-hard-way/calc.py create mode 100644 kjg/python-the-hard-way/sum.txt diff --git a/kjg/python-the-hard-way/calc.py b/kjg/python-the-hard-way/calc.py new file mode 100644 index 0000000..034b098 --- /dev/null +++ b/kjg/python-the-hard-way/calc.py @@ -0,0 +1,35 @@ +from sys import argv + +script, file_name = argv + + +def input_and_calculate_one_line(): + txt_result = '' + number = input("> ") + if number == 'q': + txt_result = '' + return txt_result + + sp_number = number.split(' ') + for i in range(len(sp_number) - 1): + txt_result = txt_result + sp_number[i] + '+' + + sp_number = list(map(int, sp_number)) + result = sum(sp_number) + txt_result = txt_result + str(sp_number[-1]) + '=' + str(result) + '\n' + return txt_result + + +def editor(): + txt = open(file_name, 'w') + while True: + output = input_and_calculate_one_line() + if output == '': + txt.close() + exit(0) + else: + print(output) + txt.write(output) + + +editor() diff --git a/kjg/python-the-hard-way/sum.txt b/kjg/python-the-hard-way/sum.txt new file mode 100644 index 0000000..8707584 --- /dev/null +++ b/kjg/python-the-hard-way/sum.txt @@ -0,0 +1,3 @@ +23+45+23+4+1+54=150 +2+3+4+6+7+8=30 +9+8+7+7+8+0=39