python shopping cart help needed

Question:

im new do python and doing a shopping cart project for practice but running into an issue with my code

shopping_list = [] # make this an empty list

dairy_cat = [['1','Milk', '$2.30'],['2','Butter', '$4.50'],['3','Eggs' ,'$3.40'],['4','cheese_slices', '$3.15'],['5','Evaporated_Milk_Creamer', '$1.40'],['6','Milo ','$12.50'],['7','Biscuits' ,'$5.30'],['8','Yogurt', '$0.95']]

total price = []

def dairy_List():

    print()

    print("------ Dairy LIST ------")
    for i in dairy_cat:
        print("* " + i[0],' ' ,end=' ')
        print(i[1],end=' ')
        print(i[2])
    item = input("Enter the item you wish to add to the shopping cart: ")
    quantity = int(input('how many do you want?'))
    while quantity > 0:
        for i in dairy_cat:
            if item in i[1]:
                shopping_list.append(item)
                total_price.append(i[2])
                print(item + " has been added to the shopping list.")
            else:
                print('you did not enter a vaild selection')
        quantity -=1

I expected that when I input Milk that the output will be ‘Milk has been added to your shopping list’ and milk being appended to the shopping_list list and its price being added to the total_price list. However, this is how it runs:

Enter the item you wish to add to the shopping cart: Milk

how many do you want?2

Milk has been added to the shopping list.

you did not enter a vaild selection

you did not enter a vaild selection

you did not enter a vaild selection

Milk has been added to the shopping list.

you did not enter a vaild selection

you did not enter a vaild selection

you did not enter a vaild selection

Milk has been added to the shopping list.

you did not enter a vaild selection

you did not enter a vaild selection

you did not enter a vaild selection

Milk has been added to the shopping list.

you did not enter a vaild selection

you did not enter a vaild selection

you did not enter a vaild selection

No error messages and the same happens with all other inputs.

Asked By: hailice

||

Answers:

please change your code like this:

shopping_list = [] # make this an empty list

dairy_cat = [['1','Milk', '$2.30'],['2','Butter', '$4.50'],['3','Eggs' ,'$3.40'],['4','cheese_slices', '$3.15'],['5','Evaporated_Milk_Creamer', '$1.40'],['6','Milo ','$12.50'],['7','Biscuits' ,'$5.30'],['8','Yogurt', '$0.95']]

total_price = []

def dairy_List():

    print()

    print("------ Dairy LIST ------")
    for i in dairy_cat:
        print("* " + i[0],' ' ,end=' ')
        print(i[1],end=' ')
        print(i[2])
    item = input("Enter the item you wish to add to the shopping cart: ")
    quantity = int(input('how many do you want?'))
    valid = False # add a variable to check valid
    while quantity > 0:
        for i in dairy_cat:
            if item == i[1]:
                shopping_list.append(item)
                total_price.append(i[2])
                print(item + " has been added to the shopping list.")
                valid = True
                break
        quantity -=1
    if valid == False: # only display once
        print('you did not enter a vaild selection')
        

dairy_List()
Answered By: Rickyxrc
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.