-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathcardrun.py
More file actions
50 lines (42 loc) · 1.13 KB
/
cardrun.py
File metadata and controls
50 lines (42 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# coding:utf-8
'''
欢迎参加黄哥python远程视频培训,
帮你完成从不会写代码到会写代码解决问题的过渡。
https://github.com/pythonpeixun/article/blob/master/index.md
咨询qq:1465376564
黄哥Python培训 黄哥改写
Python 3
P246页代码
'''
def safe_float(object):
'safe version of float()'
try:
retval = float(object)
except (TypeError, ValueError) as diag:
retval = str(diag)
return retval
def main():
'handles all the data processing'
log = open('cardlog.txt', 'w')
try:
ccfile = open('carddata.txt', 'r')
except IOError as e:
log.write('no txns this month\n')
log.close()
return
txns = ccfile.readlines()
ccfile.close()
total = 0.00
log.write('account log:\n')
for eachTxn in txns:
result = safe_float(eachTxn)
if isinstance(result, float):
total += result
log.write('data... processed\n')
else:
log.write('ignored: %s' % result)
print('$%.2f (new balance)' % (total))
log.close()
if __name__ == '__main__':
main()