【问题标题】:Stopping a function with a while loop when a certain condition is met满足特定条件时使用 while 循环停止函数
【发布时间】:2013-10-17 09:14:42
【问题描述】:
places= ["Home","In-n Out Burger", "John's house", "Santa Monica Pier", "Staples center",  "LA Dodgers stadium", "Home"]
def placesCount(places):
    multi_word = 0
    count = 0
    while True:
        place = places[count]
        if ' ' in place and place!='LA Dodgers stadium' **""" or anything that comes after LA dodgers stadium"""** :
            multi_word += 1
        if '' in place and place!='LA Dodgers stadium' """ **or anything that comes after LA dodgers stadium**""":
            count += 1
    print (count, "places to LA dodgers stadium"),  print (multi_word)
placesCount(places)

我基本上想知道在这种情况下,当 while 循环到达列表的某个元素(“LA Dodgers Stadium”)时,如何阻止它添加到列表中。在到达列表的该元素后,它不应添加任何内容。我知道我以前问过这个问题,但我没有得到正确的答案。

【问题讨论】:

    标签: python loops while-loop nested-loops


    【解决方案1】:

    您可能希望更改循环条件。而不是while True试试:

    place = places[0]
    while place != "LA Dodgers Stadium" and count < len(places):
    
        if ' ' in place:
            multi_word += 1
    
        count += 1
        place = places[count]
    

    编辑:可能更好的写法是:

    for place in places:
        if place == "LA Dodgers Stadium": break
        if ' ' in place: multi_word += 1
    

    【讨论】:

    • 和 count
    • @user2821664: 如果计数超过places 的最高索引号,count &lt; len(places) 将停止循环。
    【解决方案2】:

    (伪代码)

    flag = true
    
    while (flag)
        ...
        if (place == LA Dodgers stadium)
        {
            flag = false
        }
    

    【讨论】:

      【解决方案3】:
      running = True
      
      while running:
      
          user_input = raw_input("cmd> ")
          if user_input == "q":
              running = False
      

      while True:
      
          user_input = raw_input("cmd> ")
          if user_input == "q":
              break
      

      看看hereherehere:)

      【讨论】:

        【解决方案4】:

        嗯,这样简单的事情最快的方法是:

        places.index('LA Dodgers Stadium')
        

        如果不存在会引发 ValueError 异常,但如果存在则返回索引。那你就可以了

        while count < dodgersindex:
        

        为您的循环。更好的是,你可以这样做:

        while place in places[:dodgersindex]:
        

        这让您可以直接处理单词,而不是索引 - 更 Pythonic。

        作为更一般的情况。你会使用continuebreak 来中断执行......区别是这样的:

        x = -1
        while x < 100:
            x += 1
            if x % 2 == 0:
                continue
            if somearray[x] > 100:
                break
        

        continue 的意思是“不要尝试这个循环迭代的其余部分,继续下一个” - 对于第一项,它会检查是否有 x%2 == 0,看看是否有,甚至不看看看somearray[x] &gt; 100。但它只会进入下一个条目(这将导致 x 递增到 1)。但是,break 会导致整个循环退出……如果在 entry 51,我们发现 somearray[51] &gt; 100,我们不会检查其余的项目,而是会立即退出。

        【讨论】:

          【解决方案5】:

          正如我在上一个问题中看到的,print(placesCount) 不起作用,因为 placesCount 是一个不返回任何内容的函数。而且您正在尝试打印一个函数对象,而不是函数返回的内容。

          在函数末尾添加return。应该管用。 打印语句应该是print(placesCount(argument))

          但是,我建议你不要用文件遍历你的列表来实现你现在正在做的事情。

          您可以使用列表推导来做到这一点。这通常比手动循环列表更快。

          def placesCount(places):
              last_index = places.index('LA Dodgers stadium')
          
              multi_word = len([item for item in places[:last_index] if ' ' in item])
              count = len(places[:last_index])
              return count + "places to LA dodgers stadium\n" + (multi_word)
          
          print(placesCount(places))
          

          如果你想使用while循环。

          def placesCount(places):
              count = 0
              while True:
                  place = place[count]
                  multi_word += 1 if ' ' in place else 0
                  if place == 'LA Dodgers stadium':
                      break
                  count +=1
              return count + "places to LA dodgers stadium\n" + (multi_word)
          
          print(placesCount(places))
          

          【讨论】:

            猜你喜欢
            • 2019-01-29
            • 2013-11-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-14
            • 1970-01-01
            相关资源
            最近更新 更多