Issue
My code almost works:
number = int(input("Please type in a number: "))
a = 2
b = 1
while a in range(number) or b in range(number):
print(a)
print(b)
a += 2
b += 2
with input 6 it works as it should:
Please type in a number: 6
2
1
4
3
6
5
but with input 5 it doesn't and show the sequence 2 1 4 3
instead 2 1 4 3 5
Solution
In the for loop, we are iterating over all the odd numbers up to n (1, 3, 5 ...) and printing the pair of consecutive numbers ( i+1 - even no. first ). As we only wanted numbers up to n to print, we're checking if i+1 is less than or equal to n (p.s. inside for loop the value of i is always less than or equal to n so we just needed to make sure if i+1 matches that condition too ).
n = int(input())
for i in range(1, n+1, 2):
if i+1 <= n:
print(i+1)
print(i)
Answered By - Nick
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.