Python EOFError 中出现意外输出

问题描述 投票:0回答:1

首先,我是新来的,如果发错地方了,抱歉。

我的代码:

menu = {
    "baja taco": 4.25,
    "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
}

order = []


def total_order(order):
    total_sum = 0
    for i in order:
        total_sum = total_sum + menu[i]
    return total_sum


try:
    while True:
        item = input("item: ")
        item = item.lower()
        if item in menu:
            order.append(item)
except EOFError:
    pass


total = total_order(order)
print("${:.2f}".format(total))

我正在服用CS50P,但我陷入了这个问题。 输出是

`` 项目:


- and then it prints the total cost

However, I need it to only print the total cost when the user enters ctrl d.
I "solved" this issue in another similar program by just removing the "Item: " from the input(). But in this case the checker expects the "Item: " when reprompt happens.
Please, help me! 
python output eoferror
1个回答
0
投票

我认为你只需要在打印总成本之前打印几行空行:

total = total_order(order)
print("\n\nTotal: ${:.2f}".format(total))

通过此更改,您的代码会生成如下内容:

item: nachos
item:

Total: $11.00
© www.soinside.com 2019 - 2024. All rights reserved.