d=int(input("Decreasing number: "))
i=int(input("Increasing number: "))
def i_d (n,m):
c=0
for f in range (100,0,-n):
for c in range (0,100,m):
print (f,c)
print ()
i_d(d,i)
这是我的程序,它应该给我两个数字列表,一个递减,另一个递增。
例如:
d=60
i=40
它应该打印:
100 0
40 40
40 80
相反,它打印:
100 0
100 40
100 80
40 0
40 40
40 80
您正在使用嵌套循环,这意味着您正在为递减范围的每个值在递增范围内迭代一次。您需要使用该函数的单个循环,zip
以便并行迭代每个范围。
for f, c in zip(range(100,0,-n), range(0, 100, m)):
print (f, c)
要处理不同长度的序列,根据需要重复较短序列的最后一个元素,请使用itertools.zip_longest
. 您需要单独跟踪每个序列的最后一个值,以便您可以重用它。
for f, c in itertools.zip_longest(range(100, 0, -n), range(0, 100, m)):
if f is None:
f = last_f
elif c is None:
c = last_c
print(f, c)
last_f = f
last_c = c
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句