Search code examples
pythonloopswhile-looppause

How to pause a loop print in python


I have the following code:

i = 0
numbers = []

while i < 6:
    print ("At the top i is %d" % i)
    numbers.append(i)

    i = i + 1
    print ("Numbers now: ", numbers)
    print ("At the bottom i is %d" % i)


print ("The numbers: ")

for num in numbers:
    print (num)

If I import sleep it will slow it down, but how can I pause it so it proceeds when I want it to?


Solution

  • Just add input() where you want it to pause.

    i=0
    numbers = []
    while i < 6:
        print("At the top i is %d" % i)
        numbers.append(i)
    
        i = i + 1
        print("Numbers now: ", numbers)
        print("At the bottom i is %d" % i)
        input() # add it here for example.
    
    
    print("The numbers: ")
    
    for num in numbers:
        print(num)