Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何运行多个while循环,并在每次循环结束后关闭?_Python_Loops_While Loop_Infinite Loop - Fatal编程技术网

Python 如何运行多个while循环,并在每次循环结束后关闭?

Python 如何运行多个while循环,并在每次循环结束后关闭?,python,loops,while-loop,infinite-loop,Python,Loops,While Loop,Infinite Loop,我对Python(以及一般的编程)相当陌生,我想知道是否有一种方法可以同时运行两个或多个while循环,这样在一个循环的每一轮之后,它都会切换到另一个循环,而不必在第0轮重新开始。以下是我目前掌握的情况: def loop1(): rounds = 0 while True: if rounds == 1: print('A') elif rounds == 2: print('B') else: rounds = 0

我对Python(以及一般的编程)相当陌生,我想知道是否有一种方法可以同时运行两个或多个while循环,这样在一个循环的每一轮之后,它都会切换到另一个循环,而不必在第0轮重新开始。以下是我目前掌握的情况:

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(虽然我想你可能已经知道了?)

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

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

在我看来,您并不是真的想要运行两个循环,而是想要在单个循环中执行两件事情。比如:

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')

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

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')

为什么不把它们合并成一个循环呢

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

为什么不把它们合并成一个循环呢

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

您有一个没有break或return语句的无限循环。最好的办法是将while替换为范围内(3)个回合的while


用return语句替换rounds=0也会起作用。

您有一个无限循环,没有break或return语句。最好的办法是将while替换为范围内(3)个回合的while


用返回语句替换rounds=0也会起作用。

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

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

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

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

忽略有两个单独函数的原因(如果有一个函数,则应将其放在问题中),可以使用单个循环和
itertools
模块来实现这一点

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

忽略有两个单独函数的原因(如果有一个函数,则应将其放在问题中),可以使用单个循环和
itertools
模块来实现这一点

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

我会这样做:

from itertools import cycle

cycler = cycle(zip((1, 1, 2, 2), 'AaBb'))

for round, letter in cycler:
    # infinite loop-body here

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

我会这样做:

from itertools import cycle

cycler = cycle(zip((1, 1, 2, 2), 'AaBb'))

for round, letter in cycler:
    # infinite loop-body here

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

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

letter = 'A'

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

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

letter = 'A'

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

听起来不错,只是我不知道那是什么意思。我真的不懂任何编程术语,除了你实际键入的单词。我真的可以用一个听起来很棒的例子,但我不知道那是什么意思。我真的不懂任何编程术语,除了你实际键入的单词。我可以举一个例子,如果你使用While-True循环,你将永远运行,除非你的循环中有一个break语句来结束它。通常,这将通过某种逻辑来完成。i、 e.
if variable>2:Break
如果您有嵌套循环,则Break将提升1级。要在python中完全中断嵌套循环,有一些方法,例如重复中断或使用return。看看下面的答案,这些答案似乎抓住了一个很好的解决方案。在两个单独的函数中使用两个无限循环有什么原因吗?从技术上讲,您想要的是可能的,但这需要一些工作和对协同程序的理解。如果您使用While True循环,您将永远运行,除非您的循环中有一个break语句来结束它。通常,这将通过某种逻辑来完成。i、 e.
if variable>2:Break
如果您有嵌套循环,则Break将提升1级。要在python中完全中断嵌套循环,有一些方法,例如重复中断或使用return。看看下面的答案,这些答案似乎抓住了一个很好的解决方案。在两个单独的函数中使用两个无限循环有什么原因吗?你想要的在技术上是可能的,但它需要一点工作和对协同程序的理解。当我使用时,输出是“a B a B…”。我需要它是“a a B B”是的,我回顾了你的问题并编辑了我的答案。你只需要像我上面所说的那样切换a和B。当我使用时,输出是“a B a B…”。我需要它是“a a B B”是的,我回顾了你的问题并编辑了我的答案。你只需要像我上面所说的那样切换a和B。我们有相同的想法,很好:)我们有相同的想法,很好:)