Python Programming MOOC 2021 - More Loops - Flip the pairs

599 views
Skip to first unread message

Alicia Morillo

unread,
Nov 24, 2021, 11:34:34 AM11/24/21
to mooc.fi
Hello! I need help with this exercise because I can't find the solution :'(

Please write a program which asks the user to type in a number. The program then prints out all the positive integer values from 1 up to the number. However, the order of the numbers is changed so that each pair or numbers is flipped. That is, 2 comes before 1, 4 before 3 and so forth. See the examples below for details.

Please type in a number: 5 2 1 4 3 5

Please type in a number: 6 2 1 4 3 6 5 


My code has some mistakes because when the input is 5, I also print the number 6.

number = int(input("Please type in a number: "))
for i in range(1, number+1, 2):
    print(f'{i+1}', end='')
    print(f'{i}', end='')

Help!!!!! Thanks! 

Ben Scopp

unread,
Apr 20, 2023, 7:26:06 PM4/20/23
to mooc.fi
Correct answer:

num = int(input("Please type in a number: "))
for i in range(1, num+12):
    if i < num:
        print(f'{i+1}')
    print(f'{i}')

Reply all
Reply to author
Forward
0 new messages