Python 使用while循环打印列表中的单词,直到打印出长度为4的单词

Python 使用while循环打印列表中的单词,直到打印出长度为4的单词,python,Python,我试图使用while循环打印文本文件中列表中的单词。我需要循环停止,一旦它达到一个字,是4个字符长。用我当前的代码,我得到了一个无限循环 wordsFile = open("words.txt", 'r') words = wordsFile.read() wordsFile.close() wordList = words.split() # While loop displays names based on length of words in list print("\nSelect

我试图使用while循环打印文本文件中列表中的单词。我需要循环停止,一旦它达到一个字,是4个字符长。用我当前的代码,我得到了一个无限循环

wordsFile = open("words.txt", 'r')
words = wordsFile.read()
wordsFile.close()
wordList = words.split()

# While loop displays names based on length of words in list

print("\nSelected words are:")
while words in wordList:
if len(words) != 4:
    print(words)
所需输出样本

选词如下:

  • 减弱

  • 诡计

  • 传播

  • 反驳

  • 潜在的

  • 反常的

  • 凝结

  • 溶解

  • 唠叨


如果您的字长等于4,请添加一个
分隔符。另外,它不是
而单词列表中的单词:
,它是
而单词列表中的单词:
。使用
While expression
语句时,它将迭代,直到表达式
变为
False
,但单词列表中的
单词将始终为
True
。所以你会得到一个无限循环

if len(words)==4:
    break
您将获得以下信息:

for word in wordList:
    if len(word)==4:
        break
    print(words)
for word in words:
    # will not print the first word of length 4 which breaks the loop
    if len(word) == 4:
        break
    print(word)

break
语句的作用是什么?它停止循环并执行循环后的下一行。它在
for
循环中同样有效

如果字长等于4,则添加一个
中断。另外,它不是
而单词列表中的单词:
,它是
而单词列表中的单词:
。使用
While expression
语句时,它将迭代,直到表达式
变为
False
,但单词列表中的
单词将始终为
True
。所以你会得到一个无限循环

if len(words)==4:
    break
您将获得以下信息:

for word in wordList:
    if len(word)==4:
        break
    print(words)
for word in words:
    # will not print the first word of length 4 which breaks the loop
    if len(word) == 4:
        break
    print(word)

break
语句的作用是什么?它停止循环并执行循环后的下一行。它在
for
循环中也能工作

您的大部分代码都是关闭的,似乎您不太了解循环和条件是如何工作的

请尝试以下操作:

for word in wordList:
    if len(word)==4:
        break
    print(words)
for word in words:
    # will not print the first word of length 4 which breaks the loop
    if len(word) == 4:
        break
    print(word)

您的大部分代码都是封闭的,似乎您不太了解循环和条件是如何工作的

请尝试以下操作:

for word in wordList:
    if len(word)==4:
        break
    print(words)
for word in words:
    # will not print the first word of length 4 which breaks the loop
    if len(word) == 4:
        break
    print(word)

您是否正在尝试将文本文件中的所有单词转换为字符串列表,然后通读字符串并打印包含多于或少于四个字母的所有单词?如果是这样的话,您需要循环浏览文件并将每个单词附加到单词列表中

如果不是,我建议使用continue而不是break

for word in wordList:
    if len(word) == 4:
        continue
    else:
        print(word)

您是否正在尝试将文本文件中的所有单词转换为字符串列表,然后通读字符串并打印包含多于或少于四个字母的所有单词?如果是这样的话,您需要循环浏览文件并将每个单词附加到单词列表中

如果不是,我建议使用continue而不是break

for word in wordList:
    if len(word) == 4:
        continue
    else:
        print(word)
召回率=0

对于单词列表中的单词:

while recall == 0:
    if len(word) != 4:
        print(word)
        break
    else:
        recall = 1
向来自火星(地球之外)的利昂·蒂兹问好,孩子们

召回率=0

对于单词列表中的单词:

while recall == 0:
    if len(word) != 4:
        print(word)
        break
    else:
        recall = 1

向来自火星(地球之外)的利昂·蒂兹问好,孩子们

在发布问题时,您应该尽量将代码中的问题与不相关的细节隔离开来。在这种情况下,单词列表的来源与根据长度从列表中打印单词的操作无关。在发布问题时,应尽可能将代码中的问题与不相关的细节隔离开来。在这种情况下,单词列表的来源与根据长度从列表中打印单词的操作无关。嗨,levi,我试过你的代码,它似乎仍然给我一个无限循环。即使在循环中断的情况下。有什么建议吗?谢谢。@tronicx您应该暂时使用。看我更新的答案。嗨,李维,我试过你的代码,它似乎仍然给我一个无限循环。即使在循环中断的情况下。有什么建议吗?谢谢。@tronicx您应该暂时使用。请参阅我的更新答案。如果我正确阅读了问题,则if条件应为
==4
,并且应在打印之后。在二读时,我认为您关于长度条件的看法是正确的。从示例所需输出判断,似乎长度为4的第一个单词不应打印。如果我正确阅读了问题,则if条件应为
==4
,并且应在打印之后。再次阅读时,我认为您关于长度条件的看法是正确的。从所需输出的示例判断,似乎长度为4的第一个单词不应打印。