简体   繁体   English

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

[英]I've got some code that isn't doing what I want but I'm not getting an error message

I'm doing my 20-hour coding assignment for my GCSE and I'm trying to come up with some python code to take a food order, then add up totals and return a running total for all orders until that point.我正在为我的 GCSE 做 20 小时的编码作业,我正在尝试编写一些 Python 代码来接受食品订单,然后将总数相加并返回所有订单的运行总数,直到那时。 There's a couple of problems with my current code and I'm not getting an error message so I'm not sure how to fix it to make it do what I want.我当前的代码有几个问题,我没有收到错误消息,所以我不确定如何修复它以使其按照我的意愿行事。

Here's my code: python code for ordering system这是我的代码:订购系统的python代码

The first problem is that something is going wrong when certain combinations of item references are inputted.第一个问题是当输入某些项目引用组合时出现问题。 The totals are not adding as they should, giving me a float with an unreasonable amount of decimal places yet this isn't always the case.总数没有按应有的方式添加,给我一个带有不合理数量的小数位的浮点数,但情况并非总是如此。 The other problem is that when I enter Y to place another order, it doesn't let me.另一个问题是,当我输入 Y 下另一个订单时,它不允许我。 Though when I enter N to stop ordering, it does what I want it to.虽然当我输入 N 停止订购时,它会做我想要的。

Here's the output demonstrating these problems: code that's gone wild with the adding and not allowing me to enter another order以下是演示这些问题的输出:代码随着添加而变得疯狂并且不允许我输入另一个订单

Here's the output where everything seems ok: all good这是一切似乎都很好的输出:一切都很好

I've managed to implement these two separate concepts, the Y/N to stop or proceed and the ordering code, in other bits of code yet when I try them here, it's not working.我已经设法在其他代码位中实现了这两个独立的概念,即停止或继续的 Y/N 和排序代码,但是当我在这里尝试它们时,它不起作用。 We've been looking over it for ages and still can't figure out what is going on.我们已经研究了多年,但仍然无法弄清楚发生了什么。 Any help would be appreciated!任何帮助,将不胜感激!

[edit] here's the code that I'm struggling with: [编辑] 这是我正在努力处理的代码:

>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

As @quaabaam said, please copy and paste the problematic code in the question, that way we can copy it if we need to and can then debug it.正如@quaabaam所说,请将有问题的代码复制并粘贴到问题中,这样我们就可以在需要时复制它,然后进行调试。

I'm assuming the menuItems and menuPrices are supposed to be linked, meaning that "Large all day breakfast" costs 5.50.我假设 menuItems 和 menuPrices 应该是链接的,这意味着“大型全天早餐”的费用为 5.50。 In that case it would be best to use dictionaries.在这种情况下,最好使用字典。 for example:例如:

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

This way, to get a price you just have to call, for example x = Menu["Large all day breakfast"] the price to x, in this case 5.50.这样,要获得价格,您只需调用,例如x = Menu["Large all day breakfast"] x 的价格,在本例中为 5.50。

Also, you might want to change your while ordering == True into a function, for example此外,您可能希望将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()

If you haven't yet looked into functions, I would highly suggest it since it is very vital, especially here如果你还没有研究过函数,我强烈推荐它,因为它非常重要,尤其是在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在做一个代码,但我没有看到我做错了什么。 我不断收到此错误'ValueError:int()的无效文字,基数为10:'2a' - i'm doing a code but i don't see what i'm doing wrong. i keep getting this error 'ValueError: invalid literal for int() with base 10: '2a' 决策树显示未满足我的要求 - Decision Tree display isn't doing what I want 我正在做codeacademy来学习一些python,我不断收到一个错误,问我是否创建了一个名为plane_ride_cost的函数 - I'm doing codeacademy to learn some python, and I keep getting an error asking me if I've created a function called plane_ride_cost 我尝试编码的 Python 密码生成器不工作,但没有错误消息 - Python password generator I’m trying to code isn’t working, but there’s no error message 我正在尝试在虚拟环境中安装 django 但出现一些错误,我不明白它是怎么回事 - I'm trying to install django in virtual environment but getting some error and I don't understand what is it about 我对这个网页抓取代码做错了什么? - What I'm doing wrong with this webscraping code? 这段python代码是我想要的吗? - Is this python code doing what I want it to do? 我刚刚开始使用 Python,但我的代码无法正常工作 - I've just started Python and my code isn't working 此代码未按我希望的方式打印-python - this code isn't printing as I want it to - python 当我想让我的精灵角色射击子弹时,我收到一条属性错误消息。 我究竟做错了什么? - I am getting an Attribute error message when I want to make my sprite character shoot bullets. What am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM