首页 > 解决方案 > I keep getting errors for my restaurant code?

问题描述

So basically idk I just feel like my code is not really getting anywhere. I have trouble with the list and stuff, and I need this for school. My teacher doesn't really teach, and this is what they 'taught' us so far about list arrays, but I don't really understand it. Also, I keep getting this error of:

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

My is alright, the printing turns out OK. I took out my whole menu here and just pasted where my problem occurs. Here's the part that actually works though:

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"

The main problem is in the while statements and if statements, and how it doesn't print my order in the end. I already tried changing my code, for example:if order == "coffee": to instead if order == "1": so I could make it easier so user doesn't have to type out the whole word? And also I tried taking out the tot=tot+... just to see. I have no idea, my teacher just told us to do that, but I don't think this format is quite right.

    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)

And when the append() error doesn't appear, and the code actually 'works' when I change it back, it just ends there after 'done' and doesn't print anything after. I'm sorry if this seems really confusing, I just think this whole code is a mess.

标签: pythonstringlistappend

解决方案


list.append("coffee")

should be

food.append("coffee")

and this should be everywhere in your code wherever you are using list. list is a built-in type in Python


Also, the following code (the last loop to print items)

     for item in food:
        print(order)

should be

    for item in food:
        print(item)

else it will just print the last entered order by the user.


推荐阅读