Skip to content
Open
Changes from 1 commit
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
13 changes: 7 additions & 6 deletions Python-Home-Challenges/recurring word.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@

where_file = input('Enter the file route: ')
my_file = open(where_file,"r")
print("Your file text is:")
for x in my_file:
print(x)
print("---------------")
words_in_text = dict()
my_file = open(where_file,"r")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you dont need to open file again, also, file must be closed after ward. best practice is to use "open...with" when working with files.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

roger that!
will be fixed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you dont need to open file again

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fix.
strange, i was sure that i removed it.

for line in my_file:
Expand All @@ -18,5 +14,10 @@
for key in list(words_in_text.keys()):
print(key, ":", words_in_text[key])
print("---------------")
print (max(words_in_text.values()))

max1=0
for highest in list(words_in_text.keys()):
if max1 < words_in_text[highest]:
max1 = words_in_text[highest]
key1 = highest
print ("The most recurring word is: ", key1," with ",max1, " times")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

it is much better to use f' string. easier to read your code

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed.

my_file.close()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you should close file as soon as you done with reading it, or better - use "with...open..."

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Changed to with open.