【问题标题】:Why am I not getting output for the following code?为什么我没有得到以下代码的输出?
【发布时间】:2019-05-05 23:03:04
【问题描述】:

我在 jupyter notebook 中没有得到以下代码的输出。 这段代码基本上检查单词的长度并将它们打印出来。 我发现在“new”(list) 中附加每个单词后,while 循环甚至不起作用。

我知道还有其他方法可以以更简单的方式执行此操作,但我希望它能够工作。

st = 'Print every word in this sentence that has an even number of letters'
new=[]
i=0
for words in st.split():
    new.append(words)
l=len(st)
while i<=l:
    if len(new[i])%2==0:
        print(new[i])
        i=i+1

【问题讨论】:

    标签: python string python-3.x


    【解决方案1】:

    有几个问题。例如,l = len(st) 应该是 l = len(new)i &lt;= l 应该是 i &lt; li=i+1 需要去齿。

    另一方面,更好的方法是

    st = "Print every word in this sentence that has an even number of letters"
    for word in st.split():
        if len(word) % 2 == 0:
            print(word)
    

    【讨论】:

    • 你能解释一下为什么 (i
    • @AshishRaju 当然。你的句子有13个单词,由于i是从0开始的,所以你只需要上到12个,因为如果你算0,0到12实际上是13个项目。
    • @AshishRaju 所以i=i+1 行位于正确的位置,但您只需从它前面删除一个制表符(或四个空格),使其与 if 语句内联跨度>
    【解决方案2】:

    我不知道如何修复您的代码,因为它有太多错误。刚刚写了最接近可能的解决方案

    st = 'Print every word in this sentence that has an even number of letters'
    words = st.split()
    
    for w in words:
        if len(w)%2==0:
            print(w)
    

    【讨论】:

      【解决方案3】:

      问题在于i=i+1 发生在if 语句中。这意味着 i 永远不会超过 0,因为第一个单词的长度是奇数。要解决此问题,请将 i=i+1 放在 if 语句之外。

      while i<=l:
          if len(new[i])%2==0:
              print(new[i])
          i=i+1
      

      【讨论】:

        【解决方案4】:

        您收到一个无限循环,因为第一个条件不满足并且 i 不会增加。

        【讨论】:

          【解决方案5】:

          您的代码存在问题:

          st = 'Print every word in this sentence that has an even number of letters'
          new=[] # You can create the list of words here instead of a loop
          i=0
          for words in st.split():
              new.append(words)
          l=len(st)    # len(new)
          while i<=l:  # i < l (since indexing of i starts from 0 to n-1 length
              if len(new[i])%2==0:
                  print(new[i])
                  i=i+1 # this needs to be outside loop, since it will only increment if even word is found
          

          更新代码:

          st = 'Print every word in this sentence that has an even number of letters'
          new = s st.split()
          i = 0
          l = len(new)
          while i < l:
              if len(new[i])%2 == 0:
                  print (new[i])
              #else:
              #    pass
              i = i+1 
          

          【讨论】:

            【解决方案6】:
            st = "Print every word in this sentence that has an even number of letters"
            new=[]
            i=0
            for words in st.split():
                new.append(words)
            l=len(words)
            while i<l:
                if len(new[i])%2==0:
                    print(new[i])
                i=i+1
            

            此外,您可以使用以下方法执行相同的任务:

            st="Print every word in this sentence that has an even number of letters"
            [word for word in st.split() if len(word)%2==0]
            

            【讨论】:

              猜你喜欢
              • 2019-07-17
              • 1970-01-01
              • 1970-01-01
              • 2016-05-05
              • 1970-01-01
              • 2021-02-07
              • 1970-01-01
              • 2021-06-04
              • 2021-12-24
              相关资源
              最近更新 更多