Python for ML-Session 4

REFERENCE TIMESTAMP : 1:59:30

word_count1=dict()
line1=input(‘Enter the text:’) #user_input
words1=line1.split()
for word1 in words1:
word_count1[word1]=word_count1.get(word1,0)+1
bigcount is None
bigword is None
for word1,count in word_count1.items():
if count>bigcount:
bigword=word1
bigcount=count
print(bigword,bigcount)

OUTPUT:
Enter the text: this is a line of this text
text 7

Shouldn’t the output be,

this 2

since ‘this’ is appearing 2 times in the text entered, which is the most no of times?Or am I missing something?

Thanks.

Hi, Abhinav.

Here you need to take care of two things.

  1. bigcount is None

You cannot assign the variable using “is”, it gives a boolean value.

use declarations using “=”

You can use “is None” in conditionals but there is also the difference between “==” and “is” operator you need to know about it( kindly explore). there is a object level reference difference.

  1. if count>bigcount:

Here you are comparing the integer with None type. as you have declared “bigcount is None”.
Integer cannot be compared with “None” both are different datatypes.

Remember only Like data-types can be compared.

Now you just need to do some few changes here.

bigcount =0
bigword =0
and use
condition as if count>bigcount: --> This is as per your approach.

CASE 2 :- But you also have to handle the case where user will not give anything, that is None type. but still the count should be 1.
So, use below.
bigcount=None
bigword=None

and condition as
if bigcount is None or count>bigcount:
as empty space or None character also will be counted as 1.

Kindly check the difference between two.

All the best!


All the best!

1 Like

Oh! I kept on doing it myself without checking it with that shown in the video :neutral_face:
Thanks for pointing out! :pray: :+1: