Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions firstCodeInPython.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
text = open('text.txt', 'r')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opened file need to closed. best practice is to use "with...open..."

d = dict()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variable name should be meaningful

for line in text:
line = line.strip()
line = line.lower()
words = line.split(" ")

for word in words:
if word in d:
d[word] = d[word] + 1
else:
d[word] = 1
for key in list(d.keys()):
print(key, ":", d[key])

mostapperdword=max(d,key=d.get)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to python style guide, var name should look like this: most_apperd_word


print (mostapperdword)
print(max(d.values()))

print(f'The word that returns most times in the text file is: ' +mostapperdword+ ' and it returns ' ,max(d.values()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax error

5 changes: 5 additions & 0 deletions text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
mango