[Python applying learnings from 1..6] create calc.py
This commit is contained in:
parent
c8f538f9e7
commit
baeb4f858d
2 changed files with 38 additions and 0 deletions
35
kjg/python-the-hard-way/calc.py
Normal file
35
kjg/python-the-hard-way/calc.py
Normal file
|
@ -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()
|
3
kjg/python-the-hard-way/sum.txt
Normal file
3
kjg/python-the-hard-way/sum.txt
Normal file
|
@ -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
|
Loading…
Reference in a new issue