-1

I want to write a program that first reads in an input file, followed by two strings representing the lower and upper bounds of a search range. The file should be read using the file.readlines() method. The input file contains a list of alphabetical, ten-letter strings, each on a separate line. Your program should output all strings from the list that are within that range (inclusive of the bounds).

And the contents of input1.txt are:

aspiration
classified
federation
graduation
millennium
philosophy
quadratics
transcript
wilderness
zoologists

If the input is:

ammoniated
millennium

Here is my code:

f=open('input1.txt')
str1=input()
str2=input()

word_in_file= f.readlines()

for word in word_in_file:
    word=word.strip()
    if str1<=word<=str2:
        print(word)
f.close()

the output is:

aspiration
classified
federation
graduation
millennium

When I tried for another attempt, which I only removes the line word=word.strip(), my output turned into:

aspiration

classified

federation

graduation

My question is why the last string millennium is no longer included in the output?

  • I suspect that ‘millennium’ with crlf on the end is considered greater than the search limit. As an experiment try: ‘milleniun’ as the end term. – quamrana Nov 28 '20 at 23:27

2 Answers2

0

When you read lines from file like this they ends with \r\n and 'millennium\r\n' is greater than 'millennium'. You can use slicing to get rid of it. For example:

line = line[:-2]

or replace them with empty string

line = line.replace('\r\n', '')
maciek97x
  • 2,251
  • 2
  • 10
  • 21
0

The empty lines between the second output should give a hint. Without the strip() call, you're comparing strings with newlines to strings without them, meaning you're doing a comparison like this:

# str1 <= word <= str2
"ammoniated" <= "millennium\n" <= "millennium"

(Your string might end in \r\n instead of \n, depending on your platform)

According to string comparison rules, this will be false, since the string "millennium\n", with the newline, occurs lexicographically after "millennium", without it.

Anon Coward
  • 9,784
  • 3
  • 26
  • 37