sys.exit()

Python programs always terminate if they reach the EOF. But, you can force a program to terminate or exit at any point by calling the sys.exit() function.

import sys

while True:
    print('Type exit to exit.')
    response = input()
    if response == 'exit':
        sys.exit()
    print('You typed ' + response + '.')