Trying to print this pattern of numbers want this output:(if a=10)
2
9
4
7
6
5
8
3
10
1
CODE::
a=10
for i in range(1,a+1):
if(i%2==0):
print(i)
elif(a-i%2!=0):
print(a-i)
Trying to print this pattern of numbers want this output:(if a=10)
2
9
4
7
6
5
8
3
10
1
CODE::
a=10
for i in range(1,a+1):
if(i%2==0):
print(i)
elif(a-i%2!=0):
print(a-i)
As you starts form 2
I'd suggest the loop do the same, then depending if the value if odd or even, print the good things
a = 10
for i in range(2, a + 2):
if i % 2 == 0:
print(i)
else:
print(a - i + 2)
Have no idea what this problem is about, but keeping to your approach your style of code, you just need to flip every other value about, so:
a=10
for j in range(1,a+1):
i = j + 1 if j % 2 else j - 1
if(i%2==0):
print(i)
elif(a-i%2!=0):
print(a-i)
Another solution would be to run multiple iterators and run through them both:
a = 10
for i, j in zip(range(2, a + 2), range(a, 0, -1)):
print(j if i % 2 else i)
Or you can create a single list with all the items:
a = 10
print([i for l in zip(range(2, a + 2, 2), range(a - 1, 0, -2)) for i in l])
The first range
runs through the list forwards, and the second range
runs backwards in each case.