【问题标题】:How do I run multiple while loops that switch off after each loop round?如何在每个循环之后运行多个关闭的 while 循环?
【发布时间】:2015-07-25 08:36:06
【问题描述】:

我对 Python(和一般编程)相当陌生,我想知道是否有一种方法可以同时运行 2 个或多个 while 循环,以便在一个循环的每一轮之后切换到另一个循环,而无需必须从零轮开始。这是我目前所拥有的:

def loop1():
rounds = 0
while True:
    if rounds == 1:
        print('A')
    elif rounds == 2:
        print('B')
    else:
        rounds = 0
    rounds = rounds + 1

def loop2():
    rounds = 0
    while True:
        if rounds == 1:
            print('a')
        elif rounds == 2:
            print('b')
        else:
            rounds = 0
        rounds = rounds + 1

loop1()
loop2()

现在,当我这样做时,它所做的只是无限运行 loop1() 并且永远不会到达 loop2(),但我想知道一种让输出说的方法:

A
a
B
b
A
a
B
b
A
a
... and so on. 

有人知道我该怎么做吗?

还请注意,我是初学者,并没有真正掌握所有编程术语,所以请使用小词:) 提前致谢。 (我也在使用 Python 3(虽然我想你可能已经知道了?))

【问题讨论】:

  • 如果你使用 While True 循环,你将永远运行,除非你的循环中有一个 break 语句来结束它。通常,这将通过某种逻辑来完成。即if variable > 2: Break 如果您有嵌套循环,Breaks 将使您上升 1 级。要在 python 中完全跳出嵌套循环,有一些方法,例如重复 Breaking 或使用 return。查看下面的答案,这些答案似乎已经找到了一个很好的解决方案。
  • 您在两个单独的函数中使用两个无限循环是否有原因?您想要的在技术上是可行的,但需要一点工作和对协程的理解。

标签: python loops while-loop infinite-loop


【解决方案1】:

为什么不将它们组合成一个循环呢?

rounds = 0
while True:
    if rounds == 0:
        print('A')
    elif rounds == 1:
        print('a')
    elif rounds == 2:
        print('B')
    elif rounds == 3:
        print('b')
    rounds = (rounds + 1) % 4

【讨论】:

  • 当我使用输出是“A B a b ...”,我需要它是“A a B b”
  • 是的,我回顾了你的问题并编辑了我的答案。你只需要像我上面那样切换 a 和 B。
【解决方案2】:

在我看来,您并不是真的想运行 2 个循环,而是想在一个循环中做 2 件事。比如:

def loop():
  rounds = 0
  while True:
    doThing1(rounds)
    doThing2(rounds)
    rounds = rounds + 1
    # Look up the mod function for an easier way to do this
    if rounds > 2:
        rounds = 1

def doThing1(rounds):
    if rounds == 1:
        print('A')
    elif rounds == 2:
        print('B')

def doThing2(rounds):
    if rounds == 1:
        print('a')
    elif rounds == 2:
        print('b')

【讨论】:

    【解决方案3】:

    忽略你有两个独立函数的原因(如果你有一个,你应该把它放在你的问题中),你可以使用单个循环和 itertools 模块来做到这一点。

    import itertools
    for c in itertools.cycle("AaBb"):
        print(c)
    

    【讨论】:

    • 我们有同样的想法,很好:)
    【解决方案4】:

    您所描述的可以归结为状态机。机器有一个变量,我们可以称之为“字母”,因此机器有两种状态,具体取决于该变量是“A”还是“B”(您也可以将其设为 True/False 布尔变量)。它还有两个输出变量,大写字母后跟小写字母。打印当前状态后,我们改变状态并再次打印。

    letter = 'A'
    
    while True:
      if letter == 'A':
        print('A')
        print('a')
        letter = 'B'
      else:
        print('B')
        print('b')
        letter = 'A'
    

    【讨论】:

      【解决方案5】:

      你可以试试这个,我不知道它是否是你要找的,但它会有你的输出。

      rounds = 0
      rounds2= 0
      loop=1
      while True:
          if loop==1:
              if rounds == 1:
                  print('A')
              elif rounds == 2:
                  print('B')
              else:
                  rounds = 0
              rounds = rounds + 1
              loop=2
          if loop==2:
              if rounds2 == 1:
                  print('a')
              elif rounds2 == 2:
                  print('b')
              else:
                  rounds2 = 0
              rounds2 = rounds2 + 1
              loop=1
      

      【讨论】:

        【解决方案6】:

        我不了解 Python,但将每个循环定义为一个函数并在单个循环中按顺序调用它们似乎是个好主意。

        【讨论】:

        • 听起来不错,但我不知道那是什么意思。除了您实际键入的单词之外,我真的不知道任何编程术语。我真的可以举个例子
        【解决方案7】:

        你有一个没有 break 或 return 语句的无限循环。最好的办法是将 while 替换为 range(3) 中的循环。

        将rounds = 0 替换为return 语句也可以。

        【讨论】:

          【解决方案8】:

          我会这样做:

          from itertools import cycle
          
          cycler = cycle(zip((1, 1, 2, 2), 'AaBb'))
          
          for round, letter in cycler:
              # infinite loop-body here
          

          通过使用itertools.cycle,您可以保持程序的原始结构。循环的每次迭代都对应一个整数和一个字母,您通过元组解包进行分发。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-02-19
            • 2015-12-26
            • 2014-08-21
            • 2019-01-18
            • 2011-08-09
            • 2019-12-29
            • 2023-03-10
            • 1970-01-01
            相关资源
            最近更新 更多