为什么在Python中按下Control-D不会引发EOFError?

huangapple go评论81阅读模式
英文:

Why doesn't Control-D cause an EOFError in python?

问题

我正在尝试实现一个程序,允许用户下订单,提示他们输入商品,每次输入一行,直到用户输入control-d(这是结束程序输入的常见方式)。
然而,当我按下control-d时,在我的终端上会显示"^D",并且不会退出,而是当我在它之后点击Enter键时,它不会导致EOFError退出程序。

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("商品: ").title()
        except EOFError as e:
            print("\n")
            break
        except KeyError as k:
            print("发生键错误")
        else:
            if menu.get(item) != None:
                total+= menu.get(item)
                print(f'总计: ${total:.2f}')


main()

我期望在按下control-d时退出循环,但它只是继续循环。

英文:

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

答案1

得分: 2

正如@chepner提到的,
> 您的终端似乎没有将Ctrl-d解释为关闭标准输入的信号。

对于您的规格,即Windows上的VScode IDE,默认情况下它不会将Ctrl-D(*Nix)序列或Ctrl-Z(Windows)序列解释为文件结束(EOF)。

此外,对于Ctrl-C,您需要检查它是否中断了进程。 中断信号和EOF信号是不同的。对于Ctrl-C序列,print("n"); break 不会发生。

我不确定,但要在Windows上的VScode IDE中使用Ctrl-Z作为EOF,请查看此答案

英文:

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.

huangapple
  • 本文由 发表于 2023年7月14日 04:29:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定