Click here to Skip to main content
15,916,398 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
p="foo foo quux labs foo barquux".split()
d={}
s=[]
count=1
for x in p:
	if x not in s:
		d.update({x:count})
		
	    s.append(x)
	else:
		d[x]+=1
print(d)


What I have tried:

Hello In this program, I intend to count the occurrence of each word in a sentence and store it in a dictionary format.
But I get an indentation error at:
s.append(x)

I dont understand why. Please explain
Posted
Updated 11-Jul-21 8:30am

Quote:
I dont understand why. Please explain

Simple, you have mixed spaces and tab differently from previous line.
Python
p="foo foo quux labs foo barquux".split()
d={}
s=[]
count=1
for x in p:
	if x not in s:
		d.update({x:count}) # this line indentation is [tab][tab]
		
	    s.append(x) # this line indentation is [tab][space][space][space][space]
	else:
		d[x]+=1
print(d)

Visually it is the same, but not for the compiler.
 
Share this answer
 
format your code here
<a href="https://pythoniter.appspot.com">Pythoniter - Pretty Python Online Formatter</a>[<a href="https://pythoniter.appspot.com/" target="_blank" title="New Window">python code formatter</a>]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900