Skip to content
Advertisement

How to register CONTROL-D in my Python program? I want it to register as a EOFERROR when the user input control-d but it registers it as a string (^d)

x = set()

#items stored here to display by adding items from x

display_list = []

#items stored here to be counted

count_list = []

while True:
   
    items = input("> ").upper()
    if items != "Q":
        x.add(items)
        count_list.append(items)
        count_list.count(items)

    elif items == "Q":
        display_list.extend(x)
        display_list.sort()
        for items in display_list:
            print(f"{count_list.count(items)} {items}")
        exit()

Advertisement

Answer

Ctrl+D is the sequence in Linux and MacOS. For the equivalent in Windows you need to do Ctrl+Z followed by <Enter>

Then you’ll need to change your code so that instead of checking for an input of “Q”, you need to wrap your input() call in a try: / except EOFERROR: block

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement