Im trying to implement a program that enables a user to place an order, prompting them for items, one per line, until the user inputs control-d (which is a common way of ending one’s input to a program). however when I press control d, in my terminal this appears "^D" and it doesn't exit, instead when i click enter after it it doesn't cause the EOFError to exit the program.
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 = 0;
while True:
try:
item = input("Item: ").title()
except EOFError as e:
print("\n")
break
except KeyError as k:
print("key error occured")
else:
if menu.get(item) != None:
total+= menu.get(item)
print(f'Total: ${total:.2f}')
main()
I expected to exit loop when control d is pressed, but it just continues the loop
As @chepner mentioned,
Your terminal doesn't appear to be interpreting Ctrl-d as a signal to close standard input.
For your spec, which is VScode IDE on Windows, It doesn't interpret Ctrl-D(*Nix) sequence or Ctrl-Z(Windows) sequence as an EOF(End of File) in default.
Also, for Ctrl-C, you need to check whether it interrupts the process. The interrupt signal and EOF signal differ. With the Ctrl-C sequence, print("\n"); break
won't happen.
I'm not sure, but to use Ctrl-Z as an EOF in VScode IDE on Windows, check this answer.