@2 is only partially correct. Your code won't work even if you cast the input as an integer. Why? You are comparing an integer (your string input cast as an int) to a string. Furthermore, your string won't actually be the line number, but the contents of the said line: Look at this code:
list_of_words = ["This", "is", "a", "test", "of", "the", "different", "looping", "methods"]
for word in list_of_words:
print(word)
The output would be individualized lines showing the words, such as:
This
is
a
test
of...
and so on. Now, consider this chunk of code:
list_of_words = ["This", "is", "a", "test", "of", "the", "different", "looping", "methods"]
for word in range(len(list_of_words)):
print(word)
What do you think running this will produce?
If you said integers 0 to 8, you would be correct. Why? Because typing for word in list_of_words is like saying for each word in list_of_words. Whenever the loop is executed, the word becomes the next item in the list_of_words.
The range() is a little different. Instead of becoming the items in the list, it turns into an index of that item. For example, here is the third version of the code which produces the same exact output of the one shown for the first time
list_of_words = ["This", "is", "a", "test", "of", "the", "different", "looping", "methods"]
for word in range(len(list_of_words)):
print(list_of_words[word])
If you run this, you will get exactly the same output as if you would have just looped through the list directly.
So which one should I use, you ask. Well, it all depends on what you are trying to do. If you are comparing two lists, you may choose to loop through one and access the indexes of the other at the same time. An example of this will be the following:
list1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list2 = [0, 2, 1, 3, 4, 6, 5, 8, 7, 9]
#Looping through the first list
for number in range(len(list1)):
#Since we have an index and the second list is just as long as the first, we can access it like so
if list1[number] != list2[number]:
#The two items are not equal, so we print them
print("Inequality in list1 ", list1[number])
print("Inequality in list2 ", list2[number])
Run this code and you will see the numbers that do not quite match within the two lists.
So, now that you know the different types of loops, it should be pretty simple to fix your code. I encourage you to try it on your own first and then look at my solution.
Stop reading beyond this point if you haven't figured out yet:
#I will use the code from post 2
enter = int(input("enter a number"))
with open("filename.txt","r") as file:
read = file.read()
split = read.splitlines()
#Here is where our loop changes
for s in range(len(split)):
#We cannot see how long an integer is, so we get rid of that part
if s == enter:
#We print the line which is found since we already know what the user entered
#I change enter to s just to keep it more conventional, but either would work. It's a better practice to actually use your variable that you loop with to access whatever you are looping through
print(split[s])
else:
print("No matchs found")
And there we go. Your code is fixed.
There is a much faster way to do this which does not involve looping, but I will leave it up to you to try and figure out. Post on here if you are interested, I am sure either myself or somebody else could post the solution I am thinking of.