0

I've got this code:

Text = input("Copy Text here ")
WordList = Text.split(" ")
i = 0
for i in range (0, len(WordList)-1):
     i += 1
while (i <= len(WordList))  :
    if (WordList[i] == "Name" or "Name1" or "Name2") :
        print(WordList[i])
     else:
        print("No DATA")

If I run this code I get two problems: first it only prints the last entry which might be because I forgot to tell it to stop counting when "Name", "Name2" or "Name1" is found. The bigger problem is that this ends in an infinite loop and I've got no idea why.

2
  • 2
    there is no i change in your while loop.put i+=1 in the end of while. Commented Mar 19, 2015 at 11:21
  • 2
    Because inside the while loop you are never changing with the value of i which is a conditional variable, and hence the condition always remains true, Commented Mar 19, 2015 at 11:22

3 Answers 3

3

You have two loops: a for loop which does nothing but increment i each time through (which is a mistake, because for already does that for you), and a while loop which attempts to do the actual work, but doesn't run until after the for loop terminates.

In any case, using an index variable to iterate over a list is the wrong way to approach the problem in Python. Instead, you should iterate over it directly:

text = "a b c Name d e Name2 f Name1 g"

for word in text.split(" "):
    if word in ("Name", "Name1", "Name2"):
        print(word)
    else:
        print("No DATA")

Another problem in your original code is the line

if (WordList[i] == "Name" or "Name1" or "Name2") :

... which isn't doing what you think. What actually happens here is that "Name" or "Name1" or "Name2" is evaluated to "Name" (because of short circuit logic), and then that is tested for equality with WordList[i]. The right way to test if something is one of a number of values is with the in operator, as in the code above.

Sign up to request clarification or add additional context in comments.

Comments

2
text = input("Copy Text here ")
wordlist = text.split(" ")
for i in wordlist:
    if (i in ["Name", "Name1", "Name2"]):
        print (i)
    else:
        print ("No DATA")

You are over complicating the things the python handles the iterations very nicely, you dont need to increment the counter after ever iteration , Actually you don't need any sort of counter.

1 Comment

Thanks, it works and yeah, i´ve got the tendency to overcomplicate stuff. Anyway, you saved me a lot of time since I guess it would have been at least a day until I´d have said goodbye to the counter.
-2

Mix your loops together. the first loops only function is to keep adding to i until it is the length of your wordlist - 1.

The second loop will then continuously loop until i > the length of wordlist, which it will not do, unless you change the value of i in that loop.

This is also why it will print the last word, because i is always wordList - 1.

Try this instead

for x in range (0, len(WordList)):
    if ((WordList[x] == "Name") or (WordList[x] ==  "Name1") or (WordList[x] ==  "Name2")):
        print (WordList[x])
    else:
        print ("No Data")

3 Comments

if (WordList[x] == "Name" or "Name1" or "Name2"): would this work ?
Your if statement does not do what you think it does.
Apologies, didn't check before posting, my mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.