Search code examples
pythonwhile-loopflip

How to write the program that prints out all positive integers between 1 and given number , alternating between the two ends of the range?


The program should work as follow:

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

My code doesn't do the same. I think there is should be the 2nd loop, but I don't really understand how can I do it. Could you possibly give me a hint or advice to solve this task. Thanks. My code looks like this:

num = int(input("Please type in a number:"))
n=0
while num>n:
    a = num%10
    num -= a
    num = num/10
    print(a)
    n = n + 1   
print(n)

Solution

  • This should work:

    num = int(input("Please type in a number:"))
    number_list = [i+1 for i in range(num)]
    
    while number_list:
        print(number_list.pop(0))
        number_list.reverse()