我对 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(虽然我想你可能已经知道了吧?))
请您参考如下方法:
为什么不将它们组合成一个循环呢?
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
