'exiting from while true loop using EOFError dose not work exactly as expected

I am trying to solve a simple problem in python and this problem I'm using while true loop and to exit this loop I'm using EOFError or in the terminal using control + D (in Linux or mac, Ctrl + z + enter in windows)

the problem is like this, the user enter keep entering inputs and when he wants to stop, he just uses cntrol+d in Linux or mac or control+z+entre in windows

in windows it works, but in Linux it keeps giving the input and the result

here is my code

def main():
    menu = {
        "Baja Taco": 4.00,
        "Burrito": 7.50,
        "Bowl": 8.50,
        "Nachos": 11.00,
        "Quesadilla": 8.50,
        "Super Burrito": 8.50,
        "Super Quesadilla": 9.50,
        "Taco": 3.00,
        "Tortilla Salad": 8.00
    }
    total_order = 0
    while True:
        try:
            user_input = str(input("Item: "))
            the_input = user_input.title()
        except EOFError:
            print(f"Total: ${round(total_order,1)}", end='\n')
            break
        else:
            for key, value in menu.items():
                if the_input == key:
                    total_order = total_order + value


if __name__ =='__main__':
    main()

and i use another approch

def main():
    menu = {
        "baja taco": 4.00,
        "burrito": 7.50,
        "bowl": 8.50,
        "nachos": 11.00,
        "quesadilla": 8.50,
        "super burrito": 8.50,
        "super quesadilla": 9.50,
        "taco": 3.00,
        "tortilla salad": 8.00
    }

    items = []
    x = 0
    try:
        while True:
            items.append(input("Item: "))
    except EOFError:
            for item in items:
                if item in menu:
                    x = x + menu[item]
            print(f"Total: ${round(x,1)}", end='\n')

here is the input / output that i have and how it suppose it work

-----
in linux 
input :
Item: taco
Item: taco
Item: Total: $6.00 # -----> In Linux when I hit control+d it give me item and total 
It should give me only the total  
------
in windows
Item: taco 
Item: taco
Item: taco
Item: ^Z
Total: $9.00

is there a way to make it give me only the total when I use the EOFError?



Solution 1:[1]

No bug here. In the Linux/Mac terminal world, ctrl masks all but the first 5 bits of the "controlled" character and sends that to the server. For ctrl-d, that's ord("d") & 0x1f or 4. Looking in the ASCII chart, that's End of Transmission. A single character is sent and nothing is echoed back to the screen. Python converts that to an EOFError exception. Your code prints "Total..". Since the terminal has not received any other data, especially not a newline, it continues right where the cursor was when you hit ctrl-d.

Windows is different. Notice that you had to do ctrl-v\n - that is, press the enter key. That enter causes the terminal to advance to the next line. That's the difference.

You'll need to write some platform dependent code to get the same effect on both types of systems. But its pretty easy.

import platform

def platform_dependent_newline():
    """Print a newline on non-Windows systems. Useful when catching
    stdin EOF where Windows provides the newline but others don't."""
    if platform.system() != "Windows"
        print()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1