我有一些代码没有做我想做的,但我没有得到一个错误消息

2024-09-30 20:30:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的普通中等教育证书做20小时的编码作业,我试图想出一些python代码来接受一份食物订单,然后将总数相加,并返回所有订单的运行总数,直到那个时候为止。我当前的代码有几个问题,而且我没有收到错误消息,所以我不确定如何修复它,使其达到我想要的效果

这是我的代码:python code for ordering system

第一个问题是,当输入项目引用的某些组合时,出现了一些问题。总数没有按应有的方式增加,给了我一个小数位数不合理的浮动,但情况并非总是如此。另一个问题是,当我输入Y来下另一个订单时,它不允许我。虽然当我输入N停止排序时,它会执行我希望它执行的操作

下面是演示这些问题的输出:code that's gone wild with the adding and not allowing me to enter another order

这是一切正常的输出:all good

我已经在其他代码中实现了这两个独立的概念,停止或继续的Y/N和排序代码,但是当我在这里尝试它们时,它不起作用。我们已经看了很久了,仍然不知道发生了什么。任何帮助都将不胜感激

[编辑]以下是我正在努力解决的代码:

>menuItems = [' ', 'Large all day breakfast', 'Small all day breakfast', 'Hot dog', 'Burger', 'Cheese burger', 'Chicken goujons', 'Fries', 'Salad', 'Milkshake', 'Soft drink', 'Still water', 'Sparkling water']
>menuPrices = [0.00, 5.50, 3.50, 3.00, 4.00, 4.25, 3.50, 1.75, 2.20, 2.20, 1.30, 0.90, 0.90]
>orderTotal = 0 #resets the order total so that the total is accurate
>runningTotal = float(0.0)
>orderWords = 'Order: '
>orderItem = 1
>ordering = True
>while ordering == True:
>    while orderItem != 0:
>       orderItem = int(input('Please list the item reference number: '))
>       orderTotal = orderTotal + (menuPrices[orderItem])
>       orderWords = orderWords + ' ' + (menuItems[orderItem])
>       runningTotal = runningTotal + (menuPrices[orderItem])
>   else:
>       print(orderWords)
>       print('Your total is: £', orderTotal)
>       ordering = False
>else:
>   proceed = str(input('Do you want to place another order (Y/N)? '))
>   if proceed == 'Y':
>       ordering = True
>   if proceed == 'N':
>       ordering = False
>       print('Running total: £', runningTotal)
>   else:
>       ordering = True

Tags: the代码订单trueorderallelsetotal
1条回答
网友
1楼 · 发布于 2024-09-30 20:30:14

正如@quaabaam所说,请复制并粘贴问题中有问题的代码,这样我们可以在需要时复制它,然后调试它

我假设菜单项和菜单项应该是联系在一起的,这意味着“全天大早餐”的价格是5.50美元。在这种情况下,最好使用字典。例如:

Menu = {"Large all day breakfast" : 5.50, "Small all day breakfast" : 3.50, ... }

这样,要得到一个价格,您只需调用x = Menu["Large all day breakfast"]价格到x,在本例中为5.50

此外,例如,您可能希望将while ordering == True更改为函数

def ordering():
    while True:
        orderItem = int(input(...
        if orderItem != 0:
            orderTotal += menuPrices[orderItem] #x += 1 means that you increase x by 1, it's basically what you wrote but different notation
        else:
            print(orderWords)
        ...
            break
    proceed(runningTotal)


def proceed(runningTotal):
    proceed = ...
    elif proceed == "N":
        ordering()

如果您还没有研究函数,我会强烈建议您使用它,因为它非常重要,尤其是在这里

相关问题 更多 >