简体   繁体   English

Python无限while循环

[英]Python infinite while loop

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. 如果运行此代码,则会遇到两个问题:首先,它仅打印最后一个条目,这可能是因为我忘记告诉它在找到“名称”,“名称2”或“名称1”时停止计数。 The bigger problem is that this ends in an infinite loop and I've got no idea why. 更大的问题是,这以无限循环结束,我不知道为什么。

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. 您有两个循环:一个for循环,该循环什么都不做,但是每次都会递增i (这是一个错误,因为for已经为您完成了),而while循环试图进行实际的工作,但是直到运行之后的for循环终止。

In any case, using an index variable to iterate over a list is the wrong way to approach the problem in Python. 无论如何,使用索引变量遍历列表是在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] . 此处实际发生的是将"Name" or "Name1" or "Name2"评估为"Name" (由于短路逻辑 ),然后将其与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. 就像上面的代码一样,使用in运算符来测试某种东西是否为多个值之一的正确方法。

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. 您已经使python处理迭代的事情变得非常复杂,您不需要在每次迭代后增加计数器,实际上您不需要任何类型的计数器。

Mix your loops together. 将循环混合在一起。 the first loops only function is to keep adding to i until it is the length of your wordlist - 1. 第一个循环的唯一功能是一直加到i,直到它是单词列表的长度-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. 然后,第二个循环将连续循环,直到i> wordlist的长度为止,除非您在该循环中更改i的值,否则它将不这样做。

This is also why it will print the last word, because i is always wordList - 1. 这也是为什么它会打印最后一个单词的原因,因为我始终是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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM