我正在以以下方式寻找输出: 如果我的输入是
starting_value = 8
ending_value = 20
我希望输出为
8 13 ##8+(5) = 13
14 19 ##start of next iteration should be 13+1 = 14, and then 14+(5)
我为此写了一个for循环:
for i in range(8,20):
start = i
end = i+5
print(start,end)
i = end+1
但是我得到了错误的结果:
8 13
9 14
10 15
11 16
12 17
13 18
14 19
15 20
16 21
17 22
18 23
19 24
我的for循环中有问题吗,有什么更好的pythonic方法吗?
答案 0 :(得分:3)
您可以通过在范围内使用6的步长来做到这一点:
starting_value = 8
ending_value = 20
step = 5
for i in range(starting_value, ending_value, step + 1):
start = i
end = i + step
print(start,end)
输出:
8 13
14 19
答案 1 :(得分:1)
简单的换档:
for i in range(8, 20, 6):
print(i, i+5)
输出:
8 13
14 19
与预定义变量相同:
start, end, step = 8, 20, 5
for i in range(start, end, step+1):
print(i, i + step)
答案 2 :(得分:0)
尝试一下:
i = 8
while i <20:
start = i
end = i+5
print(start,end)
i = end+1
输出:
8 13
14 19
答案 3 :(得分:0)
这是我的看法:
starting_value = 8
ending_value = 20
start = starting_value
end = starting_value
while end+1 < ending_value:
end = start + 5
print(start, end)
start = end + 1
输出:
8 13
14 19