使用Eoferror剂量从真实循环中退出

发布于 2025-01-24 23:06:05 字数 1916 浏览 2 评论 0原文

我正在尝试在python中解决一个简单的问题,而我在真实循环时正在使用的这个问题,并且要退出此循环,我使用eoferror或使用控件 + d在终端中(在Linux或Mac中,Ctrl + Z + ins in In in In Windows)

问题就是这样,用户输入继续输入输入,当他想停止时,他只是在Windows中

的Windows中使用cntrol+D,或者在Windows中使用cntrol+d ,但是在Linux中,它一直在提供给予输入和结果

是我的代码

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()

,我在这里使用另一个批准

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')

是我拥有的输入 /输出以及它如何工作

-----
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

是否可以使它仅在我使用eoferror时给我总计?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮子 2025-01-31 23:06:05

这里没有错误。在Linux/Mac终端世界中,ctrl掩盖了“受控”字符的前5位,并将其发送到服务器。对于ctrl-d,那是ord(“ d”)& 0x1f4。在ASCII图表中,这就是传输的终结。发送一个字符,没有任何回声回到屏幕上。 Python将其转换为eoferror异常。您的代码打印“ Total ..”。由于该终端尚未收到任何其他数据,尤其是没有新的数据,因此当您击中ctrl-d时,它会继续前进。

Windows不同。请注意,您必须执行ctrl -v \ n - 也就是说,按Enter键。该输入导致终端前进到下一行。那是区别。

您需要编写一些依赖平台的代码,以对两种类型的系统产生相同的影响。但这很容易。

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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文