Skip to content

Commit 5158913

Browse files
committed
change logic to elminate the need for using keyword "global"
1 parent b1b1669 commit 5158913

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ def counter(item):
2929

3030

3131
def process_file(file_path):
32-
global total_lines, total_words, total_characters, file_count
33-
3432
with open(file_path, "r") as f:
3533
content = f.read()
3634

@@ -45,12 +43,12 @@ def process_file(file_path):
4543
else:
4644
print(f"{stats['lines']} {stats['words']} {stats['characters']} {file_path}")
4745

48-
total_lines += stats['lines']
49-
total_words += stats['words']
50-
total_characters += stats['characters']
51-
file_count += 1
52-
46+
total['lines'] += stats['lines']
47+
total['words'] += stats['words']
48+
total['characters'] += stats['characters']
49+
total['files'] += 1
5350

51+
total = {'lines': 0, 'words': 0, 'characters': 0, 'files': 0}
5452

5553

5654
for path in args.paths:
@@ -69,15 +67,15 @@ def process_file(file_path):
6967

7068

7169

72-
if file_count > 1:
70+
if total['files'] > 1:
7371
if args.line:
74-
print(f"{total_lines} total")
72+
print(f"{total['lines']} total")
7573
elif args.word:
76-
print(f"{total_words} total")
74+
print(f"{total['words']} total")
7775
elif args.char:
78-
print(f"{total_characters} total")
76+
print(f"{total['characters']} total")
7977
else:
80-
print(f"{total_lines} {total_words} {total_characters} total")
78+
print(f"{total['lines']} {total['words']} {total['characters']} total")
8179

8280

8381

0 commit comments

Comments
 (0)