Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b70233e

Browse files
AvgWordsInFile
1 parent 5a67d93 commit b70233e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

‎AvgWordsInFile.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Line 1
2+
# Number of Words: 3
3+
# Avg Word Length: 4.7
4+
# Line 2
5+
# Number of Words: 3
6+
# Avg Word Length: 4.7
7+
8+
with open("myFile.txt", mode="w", encoding="utf-8") as my_file:
9+
my_file.write("Hello world\n"
10+
"Godzilla is cool\n"
11+
"Today is April 6th\n"
12+
"This is a test for the python course on Udemy by Derak Banas")
13+
14+
with open("myFile.txt", encoding="utf-8") as my_file:
15+
line_num = 1
16+
while True:
17+
line = my_file.readline()
18+
if not line:
19+
break
20+
21+
char_count = 0
22+
for word in line.split():
23+
char_count += len(word)
24+
25+
print("Line {}: {}"
26+
"Number of Words: {}\n"
27+
"Avg Word Length: {}\n\n".format(line_num, line,
28+
len(line.split()),
29+
("{:.1f}".format(char_count / len(line.split())))))
30+
line_num += 1

‎myFile.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Hello world
2+
Godzilla is cool
3+
Today is April 6th
4+
This is a test for the python course on Udemy by Derak Banas

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /