2020-02-13 22:31:36

Hi guys. i intend to take input from user if written number matchs with charecter count of one of the lines in filename.txt, user sees the successfull message.

code is bellow:

enter = input("Type anumber: ")

with open("filename.txt","r") as file:
  read = file.read()
split = read.splitlines()
for s in split:
  if len(s)==enter:
    print(enter)
else:
  print("No matchs found")

2020-02-13 23:20:08

@1, you should cast the input value to an integer
either:
enter = int(input("enter a number"))
or:
enter = input("enter a number")
enter = int(enter)

Paul

2020-02-14 01:36:33 (edited by amerikranian 2020-02-14 01:38:30)

@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.

2020-02-14 09:06:37

As far as I understood though the OP wants to find which string has the amount of characters that he specified.
He's currently comparing the length of the string with the number passed.
Now, what I can say on further inspection is that when printing he could just do
print(s) instead of
print(enter)
That is, if he wants to know which word matched.

Paul

2020-02-14 21:41:11

@amerikranian

i've solved it like this:

enter = int(input("Enter a number: "))

with open("filename.txt","r") as file:
  read = file.read()
split = oku.splitlines()
for s in split:
  if len(s)==enter:
    print(s)
else:
  printNo matchs found.")