简体   繁体   English

Python 循环中的 Append

[英]Append in Python loop

I am having trouble with where to locate a list append so the values keep getting accumulated in the list as it goes through the loop.我在查找列表 append 的位置时遇到了问题,因此当它通过循环时,这些值会不断累积在列表中。 The code does the following:该代码执行以下操作:

  1. Shows Pizza Menu and Extras to the customer向顾客展示比萨菜单和附加服务
  2. Customer picks a pizza顾客挑选披萨
  3. Code asks if the customer wants any extras代码询问客户是否需要任何额外的东西
  4. When the customer is done with adding extras, the code will ask if there is another order当客户完成添加附加功能时,代码将询问是否有另一个订单
  5. When the customer is done with ordering pizza, I want code to display the list of pizzas in this order and their prices.当客户完成订购比萨饼时,我希望代码显示此订单中的比萨饼列表及其价格。

I added an empty list selected=[] and tried to append it every time it prompts the user to make a selection .我添加了一个空列表selected=[]并尝试 append 它每次提示用户进行选择 Where do I make a mistake?我在哪里犯错?

Newyork=['Mozarella','Pepperoni','Basil','Green Pepper']
Veggie=['Mozarella', 'Mushroom', 'Green Pepper', 'Onion']
Margarita=['Spicy Tomato Sauce', 'Mozarella']
BBQ=['Mozarella', 'BBQ Sauce','Grilled Chicken', 'Onion' ]
Extra=['Olive','Salami','Sausage','Corn']
extselect=[]
selected=[]

Price_newyork= 10
Price_veggie= 12
Price_margarita= 8
Price_BBQ= 15

print("Welcome to Pizza MIS")
print("---------------------------Menu--------------------------")
print("Newyork:", Newyork)
print("Veggie:", Veggie)
print("Margarita:", Margarita)
print("BBQ:", BBQ)
print("Extra's:", Extra)


def main():

    selection=input("Which pizza do you prefer?: ")
    selected=selected.append(selection)
    extra= input('Would you like to add extra ingredients? Yes or No: ')
    
    while extra== 'Yes' or extra=='yes':
        extselect=input("Enter the extra ingredient you want to add your pizza: ")
        try: 
            extselect_index=Extra.index(extselect)
            
            if selection=='Newyork' or selection=='newyork':
                Newyork.insert(0,extselect)
                print ("Here is your new selection:", Newyork)
                extra=input('Do you want to add another ingredient? (Yes or No): ')
                
            if selection=='Veggie' or selection=='veggie':
                Veggie.insert(0,extselect)
                print ("Here is your new selection:", Veggie)
                extra=input('Do you want to add another ingredient? (Yes or No): ')
                
            if selection=='Margarita' or selection=='margarita':
                Margarita.insert(0,extselect)
                print ("Here is your new selection:", Margarita)
                extra=input('Do you want to add another ingredient? (Yes or No): ')
                
            if selection=='BBQ' or selection=='bbq':
                BBQ.insert(0,extselect)
                print ("Here is your new selection: ", BBQ)
                extra=input('Do you want to add another ingredient? (Yes or No): ')
                
        except ValueError:
            print("That item was not found in the Extra list")
            extra=input('Do you want to add an extra ingredient? (Yes or No): ')
        
    try: 
        if selection== 'Newyork' or selection=='newyork':
            print("Here is your selection: ")
            print(Newyork)
            price(selection)
        if selection=='Veggie' or selection=='veggie':
            print("Here is your selection: ")
            print(Veggie)
        if selection== 'Margarita' or selection=='margarita':
            print("Here is your selection: ")
            print(Margarita)
        if selection== 'BBQ' or selection=='bbq':
            print("Here is your selection: ")
            print(BBQ)
           
        again=input('Do you want to order another pizza? (Yes or No) ')
        if again=='Yes':
            main()
        else:
            print('Bye')
    except ValueError:
           print("That item was not found in the list ")
           
def price(selection):
    if selection== 'Newyork' or selection=='newyork':
        print('It will cost USD',Price_newyork)
    elif selection== 'Veggie' or selection=='veggie':
        print('It will cost USD',Price_veggie)
    elif selection== 'Margarita' or selection=='margarita':
        print(Price_margarita)
    elif selection== 'BBQ' or selection=='bbq':
        print('It will cost USD',Price_BBQ)
    else:
        print('Enter again')
        
main()

In your code you are using whole category of pizzas as the user input whereas user will only input a single pizza from that category.在您的代码中,您使用整个比萨饼类别作为用户输入,而用户只会输入该类别中的一个比萨饼。

selection=input("Which pizza do you prefer?: ")

also in the second line you did selected=selected.append(selection) as a result selected will be None as ".append()" operation on a list returns None as it modifies the list instead of returning a new one.同样在第二行中,您执行了selected=selected.append(selection)作为结果 selected 将是 None 因为列表上的“.append()”操作返回 None 因为它修改列表而不是返回新列表。

Moving forward you are using extselect_index=Extra.index(extselect) in the while loop to check in the selected Extra item is in the Extra list and if it is not then catching the error with except block.继续前进,您在 while 循环中使用extselect_index=Extra.index(extselect)来检查选定的 Extra 项目是否在 Extra 列表中,如果它不在,则使用 except 块捕获错误。 It can be simply done by if-else statement as follows:可以简单地通过 if-else 语句来完成,如下所示:

if extselect in Extra:
      **code**
else:
      print("not found in the list")

After this you used different "if" statements with condition as "selection == 'Newyork'" as i said before used will now select a category he will take a single pizza from that category.在此之后,您使用了不同的“if”语句,条件为“selection == 'Newyork'”,正如我之前所说的,现在使用 select 一个类别,他将从该类别中取出一个披萨。 Here the condition should be as:这里的条件应该是:

if selection in Newyork:
      selected.insert(0,extselect)
      print ("Here is your new selection:", Newyork)
      extra=input('Do you want to add another ingredient? (Yes or No):')
      if extra == 'yes':
         continue
      else:
          break

above code snippet will see if selected pizza is in category Newyork and if it is true it will append the Extra topping selected to the "selected" list as it is the list we are using to maintain our selected stuff.上面的代码片段将查看所选比萨饼是否属于 Newyork 类别,如果为真,它将 append 将 Extra topping 选择到“选定”列表中,因为它是我们用来维护所选内容的列表。 you can do the same for every if statement and change other if statements to "elif" instead.您可以对每个 if 语句执行相同的操作,并将其他 if 语句更改为“elif”。

In the second try block modify the if statements the same way and also pass strings to the "price()" function instead of passing variable names directly.在第二个 try 块中,以相同的方式修改 if 语句,并将字符串传递给“price()”function,而不是直接传递变量名。 Inside the price function it tests the selection with against strings not against variables.在价格 function 内,它使用字符串而不是变量来测试选择。

It should resolve any problems you are having with the above code.它应该可以解决您在使用上述代码时遇到的任何问题。

Newyork=['Mozarella','Pepperoni','Basil','Green Pepper']
Veggie=['Mozarella', 'Mushroom', 'Green Pepper', 'Onion']
Margarita=['Spicy Tomato Sauce', 'Mozarella']
BBQ=['Mozarella', 'BBQ Sauce','Grilled Chicken', 'Onion' ]
Extra=['Olive','Salami','Sausage','Corn']
extselect=[]
selected=[]

Price_newyork= 10
Price_veggie= 12
Price_margarita= 8
Price_BBQ= 15

print("Welcome to Pizza MIS")
print("---------------------------Menu--------------------------")
print("Newyork:", Newyork)
print("Veggie:", Veggie)
print("Margarita:", Margarita)
print("BBQ:", BBQ)
print("Extra's:", Extra)

def main():

    selection=input("Which pizza do you prefer?: ")
    selected.append(selection)
    extra= input('Would you like to add extra ingredients? Yes or No: ')

    while extra== 'Yes' or extra=='yes':
        extselect=input("Enter the extra ingredient you want to add your pizza: ")
        try: 
            if extselect in Extra:
        
                if selection in Newyork:
                    extselect.insert(0,extselect)
                    print ("Here is your new selection:", selected)
                    extra=input('Do you want to add another ingredient? (Yes or No): ')
                    if extra == 'yes' or extra == 'Yes':
                        continue
                    else:
                        break
                
                if selection in Veggie:
                    extselect.insert(0,extselect)
                    print ("Here is your new selection:", selected)
                    extra=input('Do you want to add another ingredient? (Yes or No): ')
                    if extra == 'yes' or extra == 'Yes':
                        continue
                    else:
                        break
                
                if selection in Margarita:
                    extselect.insert(0,extselect)
                    print ("Here is your new selection:", selected)
                    extra=input('Do you want to add another ingredient? (Yes or No): ')
                    if extra == 'yes' or extra == 'Yes':
                        continue
                    else:
                        break
                
                if selection in BBQ:
                    extselect.insert(0,extselect)
                    print ("Here is your new selection: ", selected)
                    extra=input('Do you want to add another ingredient? (Yes or No): ')
                    if extra == 'yes' or extra == 'Yes':
                        continue
                    else:
                        break
            else:
                print(f'{extselect} is Not available')
                
        except ValueError:
            print("That item was not found in the Extra list")
            extra=input('Do you want to add an extra ingredient? (Yes or No): ')

     try: 
        if selection in Newyork:
            print("Here is your selection: ")
            print(selected)
            price('Newyork')
        elif selection in Veggie:
            print("Here is your selection: ")
            print(selected)
            price('veggie')
        elif selection in Margarita:
            print("Here is your selection: ")
            print(selected)
            price('margarita')
        elif selection in BBQ:
            print("Here is your selection: ")
            print(selected)
            price('BBQ')
     def price(selection):
         if selection== 'Newyork' or selection=='newyork':
             print('It will cost USD',Price_newyork)
         elif selection== 'Veggie' or selection=='veggie':
             print('It will cost USD',Price_veggie)
         elif selection== 'Margarita' or selection=='margarita':
             print(Price_margarita)
         elif selection== 'BBQ' or selection=='bbq':
             print('It will cost USD',Price_BBQ)
         else:
             print('Enter again')
    
     main()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM