Reputation: 31
I need help with this assignment because I'm extremely stuck (even my tutor bailed on me). I'm fairly new to python, so please don't be too brutal on me. Any help will be amazing. I'll list off everything the problem gives me...
"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 output all strings from the list that are within that range (inclusive of the bounds)."
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
classified
federation
graduation
millennium
Notes:
There is a newline at the end of the output. input1.txt is available to download. In the tests, the first word input always comes alphabetically before the second word input.
Thank you for any help! - :)
Edit - My current code:
user = open(input())
sequence1 = input()
sequence2 = input()
lines = user.readlines()
for line in lines:
line = line.strip()
if sequence1 <= line <= sequence2:
print(line)
Final edit - I appreciate everyone answering and providing explanations! The whole idea of files really confused me when it was introduced so seeing different ways of tackling this code helped me immensely. I'm using this experience to learn and I'm sorry for any confusion that I caused along the way. I also apologize because I'm very new on this site and I didn't realize how to do somethings on this website either because I panic made this post because as I mentioned, my tutor bailed on me and didn't give me a second resort. Thank you all!
Upvotes: 0
Views: 11052
Reputation: 11
***Fellow student here using the zyBooks material!
The trick to this problem is accounting for the fact that the lower and/or upper inputs may not actually be a line in the file,but rather solely just a point to start alphabetically. It also accounts for multiple lines of the file beginning and/or ending with the same first index and did not necessarily come first alphabetically.. ( eg. if "lower" was "Houston", but the file included "Harrisburg, Houston, Huntsville"
This codeblock will open the file, then loop through each line of the file and print each line whose entire range of letters alphabetically fall between the lower and upper ranges. without the index range of [0:9], "if lower[0:9] <= i[0:9] <= upper[0:9]:", the program wont account for the above.
userInput = input()
lower = input()
upper = input()
with open(userInput, 'r') as f:
lines = f.readlines()
for i in lines:
if lower[0:9] <= i[0:9] <= upper[0:9]:
print(i.strip())
Upvotes: 1
Reputation: 96
It's easier if you follow the prompt and use readlines
which returns a list. Get the index of the first word, and of the last word; slice between the two (+1 the last word index for inclusivity). Use rstrip
to get rid of the new line escaped character. input()
takes an optional parameter to prompt the user for what they want to run this against. If you need the total of lines output, like in your original code (but not in the prompt you posted?), just call len(someList)
again over the final slice, or just get the difference of lastIdx - firstIdx
file = input("file name: ")
firstWord = input("first word: ")
lastWord = input("last word: ")
with open(file, 'r') as f:
lines = f.readlines()
firstIdx = 0
lastIdx = len(lines)
for i, line in enumerate(lines):
line = line.rstrip()
if line == firstWord:
firstIdx = i
if line == lastWord:
lastIdx = i
print(lines[firstIdx:lastIdx+1])
Upvotes: 1
Reputation: 2607
updating w/ context...
Given the following as x.txt
aspiration
classified
federation
graduation
millennium
philosophy
quadratics
transcript
wilderness
zoologists
Here is the code you're looking for...
lower = "ammoniated"
upper = "millennium"
filename = "x.txt"
def wordKey(x: str) -> bool:
return lower <= x <= upper
with open(filename, "r") as f:
words = list(filter(wordKey, [word.strip() for word in f.readlines()]))
f.close()
['aspiration', 'classified', 'federation', 'graduation', 'millennium']
compact:
lower = "ammoniated"
upper = "millennium"
filename = "x.txt"
print(list(filter(lambda x: lower <= x <= upper, [word.strip() for word in open(filename, "r").readlines()])))
add some input to those variables:
lower = input("input lower bound: ")
upper = input("input upper bound: ")
filename = input("Enter filename (example: text.txt): ")
Upvotes: 2
Reputation: 162
filename = input() # 'input1.txt'
start = input() # 'ammoniated'
end = input() # 'millennium'
with open(filename, 'r') as f:
words = f.readlines()
for w in words:
if w < start or end < w:
continue
print(w)
Upvotes: 0