python break keeps closing the script after printing data [closed]

Unfortunately I can’t test your code, or additions to it, because it is not complete and self-contained. Instead, here is a stripped-down demo program that mimics the same structure:

responses = []
print('Enter as many responses as you would like. Press enter on a blank line '
      'to see output.')

while True:
  response = input('Response: ')
  if response:
    responses.append(response)
  else:
    break

print('Output:')
for response in responses:
  print(f'***{response}***')

Like your actual program, this one creates an empty list, adds to it repeatedly in a while loop, and then, after breaking out of that loop, loops through the list in a for loop to print the output. Also like yours, the program exits as soon as it’s done printing the output.

To make the whole thing repeat, put it all inside an outer while loop. Putting a call to time.sleep at the end of this outer loop will cause it to pause in between.

import time

while True:
  responses = []
  print('Enter as many responses as you would like. Press enter on a blank '
        'line to see output.')

  while True:
    response = input('Response: ')
    if response:
      responses.append(response)
    else:
      break

  print('Output:')
  for response in responses:
    print(f'***{response}***')

  time.sleep(3)

This will repeat indefinitely, until you halt it with Ctrl+C.

Leave a Comment