Skip to content

Instantly share code, notes, and snippets.

@thejqs
Last active February 3, 2020 03:16
Show Gist options
  • Save thejqs/60a30ed8d40dac980d0837ff0da25197 to your computer and use it in GitHub Desktop.
Save thejqs/60a30ed8d40dac980d0837ff0da25197 to your computer and use it in GitHub Desktop.
search the text of The Iliad
search_term = 'Ajax'
with open('iliad.txt') as in_file:
# how many times our search_term has occurred
term_count = 0
# how many lines contain at least two of our search_term
mult_term_line = 0
# lowercase because of line.lower() below -- we want to compare lowercase only against lowercase
search_term = search_term.lower()
for line in in_file:
line = line.lower()
if search_term in line:
# however many times our search_term has already occurred, add the number that are in this line
term_count += line.count(search_term)
if line.count(search_term) > 1:
# print(line)
mult_term_line += 1
if mult_term_line != 1:
times = 'times'
else:
times = 'time'
print(f'{search_term} was in The Iliad {term_count} times')
print(f'It was on the same line multiple times {mult_term_line} {times}, which will have printed out above unless the answer was zero')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment