0

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. 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. 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()
3
  • 1
    Lists are something called "mutable" objects which means they can be modified by functions without reassigning them a new value. Your append statement here is wrong selected=selected.append(selection). You just need to write selected.append(selection) no assigning it back to the variable. compciv.org/guides/python/fundamentals/lists-mutability Check the section on "In-place list methods and operations" Commented Jan 29, 2021 at 19:19
  • 1
    Also, a tip to help clean up some of your if statements is this. If selection.lower() == 'newyork' .lower() changes all charachter to lower case. This way you don't have to worry about upper/lower case letters. Commented Jan 29, 2021 at 19:21
  • @CorreyKoshnick that worked! I added total=0 at the top and tried to total+=Price_newyork for everytime a Newyork pizza is ordered and so on to find total price. I can't find where the correct place to add this code for each style of pizza. Commented Jan 29, 2021 at 19:44

1 Answer 1

1

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.

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. It can be simply done by if-else statement as follows:

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. 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. you can do the same for every if statement and change other if statements to "elif" instead.

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. Inside the price function it tests the selection with against strings not against variables.

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()
Sign up to request clarification or add additional context in comments.

3 Comments

It works. To also find the total price that will be paid, I added total=0 at the top and tried to total+=Price_newyork for everytime a Newyork pizza is ordered and so on to find total price. I can't find where the correct place to add this code for each style of pizza. @ShoaibWani
But you already have price() function for that. Why do you need to add another piece of code just for that?@BasakUlcay
The price function shows the individual prices of the pizzas, however, I want code to show the total price of the order (if two pizzas are ordered, sum up the prices).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.