-
Notifications
You must be signed in to change notification settings - Fork 11
first python program #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
file_object =open("a.txt","r") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to use full path instead of file name |
||
data=file_object.read().split() | ||
dict = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do not use "dict" as variable name, and as general guideline - var names need to be clear and meaningful |
||
for i in data: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, in python we are not using 'i' as var name. |
||
if i in dict: | ||
dict[i] = dict[i] + 1 | ||
else: | ||
dict[i] = 1 | ||
print(f'the word that appears most is: {max(dict, key=dict.get)}, And it appears {dict[max(dict, key=dict.get)]} times') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When opening file it is mandatory to close it when its no longer needed. you can use "close" method, but better implementation would be to use "with...open"