Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/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
基于2值Python的循环_Python_Python 3.x - Fatal编程技术网

基于2值Python的循环

基于2值Python的循环,python,python-3.x,Python,Python 3.x,我有两个变量 例: 然后我想根据compnt长度循环,然后根据变量first和second更改状态on或off 我想要的输出: On On On Off Off On On On Off 我目前的代码是: x,y=0,0 for i in range(0,compt): if x != first: print("On") x+=1 elif x == first and y != second: print("Of

我有两个变量

例:

然后我想根据compnt长度循环,然后根据变量first和second更改状态on或off

我想要的输出:

On
On
On
Off
Off
On
On
On
Off
我目前的代码是:

x,y=0,0
for i in range(0,compt):
   if x != first:
      print("On")
      x+=1
   elif x == first and y != second:
      print("Off")
但是上面代码的输出是

On
On
On
Off
Off
On
On
On
On
有人能帮我解决我的问题吗,谢谢你 第一个=3 秒=2 对于范围内的i(compt): 最大值=第一+第二 如果i%max_val<首先: 打印(“在”) 其他: 打印(“关闭”) 输出:

on
在…上
在…上
关
关
在…上
在…上
在…上
关
来自itertools导入周期,islice
总数=9
第一个=3
秒=2
顺序=[范围内(第一)的“开”]+[范围内(第二)的“关”]
打印(顺序)
结果=islice(循环(顺序),0,总计)
对于结果中的状态:
打印(状态)
输出:

['On', 'On', 'On', 'Off', 'Off']
On
On
On
Off
Off
On
On
On
Off

itertools的另一个变体是:

from itertools import cycle, repeat, chain

compt = 9
first = 3
second = 2

on = repeat("On", first)    # ["On", "On", ..] 
off = repeat("Off", second) # ["Off", "Off", ..]

for status in cycle(chain(on, off)): # combine on and off and repeat
    print(status)

    # break when compt is exhausted
    compt -= 1
    
    if compt <= 0:
        break

从itertools导入周期、重复、链
compt=9
第一个=3
秒=2
on=重复(“on”,first)#[on”,“on”,“on”…]
关闭=重复(“关闭”,秒)#[“关闭”,“关闭”,…]
对于循环中的状态(链(开、关)):#组合开、关并重复
打印(状态)
#当compt耗尽时中断
compt-=1

如果compt您可以使用
%
。我们有一个总的
first+second
可能性。如果我们的
i
小于
first
,则表示我们正在查找
on
,如果大于first,则希望打印
off

compt = 9
first = 3
second = 2
for i in range(compt):
    if i % (first + second) < first:
        print("ON")
    else:
        print("OFF")
compt=9
第一个=3
秒=2
对于范围内的i(compt):
如果i%(第一个+第二个)<第一个:
打印(“在”)
其他:
打印(“关闭”)
试试看

compt = 9
first = 3
second = 2
i=0
while i < compt:
    for k in range(0,first):
        i += 1
        print('on')
        if i >= compt:
            break
    for j in range(0,second):
        i += 1
        print('off')
        if i >= compt:
            break
compt=9
第一个=3
秒=2
i=0
而我=compt:
打破
对于范围内的j(0,秒):
i+=1
打印(‘关闭’)
如果i>=compt:
打破

我喜欢这个答案。如果您想一直使用itertools,则可能
islice(cycle(chain(on,off)),compt)
可以工作。另外,考虑添加一些评论。谢谢!这是islice的一个很好的观点,我没有想到这一点,但是看到@Lenormju在他的回答中使用了它,这将使它更加简洁。评论传入..我需要学习模更多。。冷却液。
compt = 9
first = 3
second = 2
i=0
while i < compt:
    for k in range(0,first):
        i += 1
        print('on')
        if i >= compt:
            break
    for j in range(0,second):
        i += 1
        print('off')
        if i >= compt:
            break