1

TL;DR: I ended up with mostly the desired result, but it is adding " - in range" or " - not in range" on a new line and I want them to be on the same line. I know I can append the list if I needed to, but the instructions don't say to do that. Any help and explanation is appreciated.

Instructions: Write a program that first reads in the name of 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 determine if the strings from the list are within that range (inclusive of the bounds) and output the results.

Ex: If the input is:

input1.txt
ammoniated
millennium

and the contents of input1.txt are:

aspiration
classified
federation
graduation
millennium
philosophy
quadratics
transcript
wilderness
zoologists

the output is:

aspiration - in range
classified - in range
federation - in range
graduation - in range
millennium - in range
philosophy - not in range
quadratics - not in range
transcript - not in range
wilderness - not in range
zoologists - not in range

Notes:

End the output with a newline. In the tests, the first word input always comes alphabetically before the second word input.

MY CODE:

input_file = input() 
upper = input() 
lower = input() 

with open(input_file, 'r') as f:  
    contents = f.readlines()

for i in range(len(contents)):
    if  (contents[i] >= upper) and (contents[i] <= lower):
        print(contents[i],' - in range')
    else:
        print(contents[i],' - not in range')
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • use `contents[i].strip()` to remove the newlines. – Barmar Jun 27 '23 at 20:25
  • Welcome to Stack Overflow! Get out of the habit of using `for index in range(len(list)):`. Use `for item in list:` or `for index, item in enumerate(list):` – Barmar Jun 27 '23 at 20:25
  • I always prefer: `contents = f.read().splitlines()` to remove the newlines where speed/size don't matter.~ – BeRT2me Jun 27 '23 at 20:26
  • Does this answer your question? [How to read a file line-by-line into a list?](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list) – slothrop Jun 27 '23 at 20:31
  • Thank you for the advice yall! The .strip was the secret ingredient. I also had to add +'\n' to the upper and lower in the if statement since the list had a new line after every word and it caused my lower word to not be in range when it needed to be. – user22141540 Jun 28 '23 at 03:17

2 Answers2

0

Each of the items in the list comes with it's own newline. That is, if the input file is

aardvark
badger
caterpillar
...

then readlines() will return ["aardvark\n", "badger\n", "caterpillar\n", ...].

You can either remove these via

with open(input_file, 'r') as f:  
    contents = [content.strip() for content in f.readlines()]

or strip upon printing via

print(contents[i].strip(),' - in range')
print(contents[i].strip(),' - not in range')
davidlowryduda
  • 2,404
  • 1
  • 25
  • 29
0
input_file = input() 
upper = input() 
lower = input() 

with open(input_file, 'r') as f:
    # Split your contents into the list of words.
    contents = f.read().splitlines()

# Sort upper, lower, and your contents into a single list.
sorted_contents = sorted(contents + [upper, lower])

# Find where the index of upper and lower would be.
upper_index = sorted_contents.index(upper)
lower_index = sorted_contents.index(lower)

for i in range(len(contents)):
    # If it's in the created range, print it so.
    if i in range(upper_index, lower_index):
        print(contents[i] + " - in range")
    else:
        print(contents[i] + " - not in range")
BeRT2me
  • 12,699
  • 2
  • 13
  • 31