【问题标题】:I keep getting errors for my restaurant code?我的餐厅代码不断出现错误?
【发布时间】:2019-06-16 01:36:23
【问题描述】:

所以基本上我只是觉得我的代码并没有真正到达任何地方。我对清单和其他东西有疑问,我需要这个上学。我的老师并没有真正教过,这是他们迄今为止“教”给我们的关于列表数组的内容,但我并不真正理解它。另外,我不断收到以下错误:

TypeError: append() takes exactly 2 arguments (1 given) on line 31 

我没问题,打印结果正常。我在这里拿出我的整个菜单,然后粘贴到我出现问题的地方。这是实际工作的部分:

print "[Welcome to Python Cafe!]"
print ('\n')

print "1) Menu and Order"
print "2) Exit"
choice=input("What would you like to do? ")

print ('\n')
if choice == "1":
        print "-T H E  M E N U-"
        print " DRINKS "
        print "1. Coffee: $2.50" 
        print "2. Hot Cocoa: $2.30"
        print "3. Tea: $1.50"
        print " FOOD "
        print "4. Bagel: $1.50"
        print "5. Donut: $1.00"
        print "6. Muffin: $1.50"

主要问题出在while 语句和if 语句中,以及它是如何实现的 最后不打印我的订单。我已经尝试更改我的代码,例如:if order == "coffee": 改为 if order == "1":,这样我可以让它更容易,这样用户就不必输入整个单词?我还尝试取出tot=tot+... 只是为了看看。我不知道,我的老师只是告诉我们这样做,但我认为这种格式不太正确。

    if choice == "1":
        print ('\n') 
        food=[]
        order=0
        while order != "done":
            order=input("What's your order?  ")
            if order == "coffee":
                    list.append("coffee")
                    tot=tot+2.50
            else: 
                if order == "hot cocoa":
                        list.append("hotcocoa")
                        tot=tot+2.30
                if order == "tea":
                        list.append("tea")
                        tot=tot+1.50
                if order == "bagel":
                        list.append("bagel")
                        tot=tot+1.50
                if order == "donut": 
                        list.append("donut")
                        tot=tot+1.00
                if order == "muffin":
                        list.append("muffin")
                        tot=tot+1.50
        print ('\n') 
        print "Here's your final order:"
        for item in food:
            print(order)

append() 错误没有出现,并且当我将其改回代码实际上“工作”时,它只是在“完成”之后结束并且之后不打印任何内容。如果这看起来真的很混乱,我很抱歉,我只是认为整个代码是一团糟。

【问题讨论】:

  • 随便输入food.append(...)...

标签: python string list append


【解决方案1】:

您正在尝试使用list.append() 方法未绑定list 是内置类型,.append() 是在 list 实例 上使用时允许您将值附加到该列表的方法。但是您还没有告诉list.append() 要附加到哪个列表实例。

您通常会在特定列表实例上调用方法

food.append("coffee")

这仍然是相同的list.append() 方法,但现在它绑定food 列表实例,然后Python 确保调用list.append(food, "coffee")。您通常不会直接使用list.append()(一方面,它会阻止子类覆盖append() 方法),让Python 在这里找出正确的绑定。

现在在您使用list.append("...") 的任何地方执行此操作。

您还在“这是您订购的”循环中打印出错误的变量:

for item in food:
    print(order)

你从来没有给tot一个初始值;你在那里有order=0,但你也使用order来存储客户输入!你可能把ordertot 弄糊涂了。

对于food 列表中的每一项,您要打印item,而不是客户订购的最后一件商品:

for item in food:
    print(item)

或者,如果您想变得非常漂亮和令人印象深刻,请使用一些高级 Python 语法并在一个步骤中打印整个列表,并在其间添加换行符:

print(*food, sep="\n")

您可能希望使用字典定义从食品到价格的映射:

prices = {
    "coffee": 2.50,
    "hot cocoa": 2.30,
    "tea": 1.50,
    "bagel": 1.50,
    "donut": 1.00,
    "muffin": 1.50,
}

这使得检查正确的订单以及将来向菜单中添加更多项目变得更加容易!现在您可以使用:

tot = 0
food = []
while True:
    order = input("What's your order?  ")
    if order == 'done':
        break
    if order not in prices:
        print("Sorry, we don't have any", order)
    else:
        food.append(order)
        tot = tot + prices[order]

print("Here is your order:", *food, sep="\n")
print("That'll be", tot)

请注意我是如何为您没有价格的订单偷偷附加消息的。此外,上面添加了"hot cocoa",而不是"hotcocoa"food 列表中,如果这可能对您有问题,请考虑到这一点。

【讨论】:

    【解决方案2】:
    list.append("coffee")
    

    应该是

    food.append("coffee")
    

    无论您使用list,这都应该出现在您的代码中。 list 是 Python 中的内置类型


    另外,下面的代码(打印项目的最后一个循环)

         for item in food:
            print(order)
    

    应该是

        for item in food:
            print(item)
    

    否则它只会打印用户最后输入的订单。

    【讨论】:

      【解决方案3】:

      list 替换为要附加到的food

      order 替换为tot 以节省价格

      final 订单替换为item 要打印的项目

      【讨论】:

        【解决方案4】:

        只是一个答案,但格式更好。
        只需输入:

        food.append(...)
        

        而不是

        list.append(...)
        

        无处不在。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-28
          • 1970-01-01
          • 1970-01-01
          • 2021-07-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多