Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python shopping cart help needed

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading