Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Word count
#2
In python,

for word in dataSplit :
will iterate over every word in the data, so...

def searchText ():
	print("Please enter a word that will be used to search a text file")
	search_word = input()
	textFile = open ('assignment8.txt', "r")
	data = textFile.read()
	dataSplit = data.split ()
	count = 0
	for word in dataSplit :
		if word == search_word :
			count = count + 1
	print (count)

searchText ()
will count every occurrence of the search word. You almost had it.
Reply


Messages In This Thread
Word count - by vanjoe198 - Mar-15-2021, 09:53 PM
RE: Word count - by BashBedlam - Mar-16-2021, 12:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  sys.stdin to do a word count based on user entry Kaltex 3 3,734 Jul-19-2020, 01:54 PM
Last Post: deanhystad
  count unique letters in a word sunny_awesome 4 8,777 Jun-06-2019, 07:15 PM
Last Post: kotter
  Count Letters in a Sentence (without using the Count Function) bhill 3 5,252 Jun-19-2018, 02:52 AM
Last Post: bhill

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020