在 Python 循环中追加

Append in Python loop

我在查找列表追加位置时遇到问题,因此值在列表中不断累积,因为它经过循环。 该代码执行以下操作:

  1. 向客户显示披萨菜单和附加服务
  2. 顾客选择披萨
  3. 代码询问客户是否需要任何额外服务
  4. 当客户添加完附加功能后,代码会询问是否还有其他订单
  5. 当客户完成比萨饼的订购后,我希望代码显示此订单中的比萨饼列表及其价格。

我添加了一个空列表 selected=[] 并尝试在每次提示用户进行 selection 时附加它。我哪里出错了?

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()

在您的代码中,您使用整个类别的比萨饼作为用户输入,而用户只会输入该类别中的一个比萨饼。

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

同样在第二行你做了 selected=selected.append(selection) 结果 selected 将 None 作为列表上的“.append()”操作 returns None 因为它修改列表而不是返回一个新列表。

接下来,您将在 while 循环中使用 extselect_index=Extra.index(extselect) 来检查 selected Extra 项目是否在 Extra 列表中,如果不在,则使用 except 块捕获错误。可以简单的通过if-else语句来完成,如下:

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

在此之后,你使用了不同的“if”语句,条件为“selection == 'Newyork'”,正如我之前所说,现在 select 他将采取一个类别该类别的单个披萨。这里的条件应该是:

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

以上代码片段将查看 selected 披萨是否属于 Newyork 类别,如果是,它将附加 Extra topping selected 到“selected”列表因为它是我们用来维护 selected 内容的列表。您可以对每个 if 语句执行相同的操作,并将其他 if 语句更改为“elif”。

在第二个 try 块中,以相同的方式修改 if 语句,并将字符串传递给“price()”函数,而不是直接传递变量名。在价格函数内部,它使用字符串而不是变量来测试 selection。

它应该可以解决您在使用上述代码时遇到的任何问题。

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()