Python Forum

Full Version: no popin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i m working on a shopping cart. i post the whole code because maybe i made a mistake on the top that effect my problem.
by option 3 i need to pop an item but its a mess. i need to pop not remove because the other items need to take the place from the removed on.
so by option 3 i need to show what was all addet to the cart and then have the option to remove oneby tipping the number of the index from this item.
right now it only shows me one item even when there are more stored and then it ask me again to remove an item but it should go back to the main options
. it would be also great that when the user puts in a wrong number it shows a message ans the lloops back to ask again what he wants to remove
do you guysy have any ideas how to fix this, i m to new to all of this.
thanks you all

#shopping cart with the 5 options

items = []
prices = []
ite_pri = items + prices

index = 0
name = None
cost = 0

print('Welcome to the Shopping Cart Program!') 

while name != 'quit':
    
    print()
    print('Please select one of the following: ')
    print('1. Add item \n2. View cart \n3. Remove item \n4. Compute Total \n5. Quit')
    
    option = input ('Please enter an action: ')
    #1. option is to add a item stored in list items and prices, aks how for price and new: how many
    if option == '1':
      name = input('What item would you like to add? ')
      cost = input(f"What is the price of '{name}'? ")
      quantity = input(f"How many '{name}'? ")
      print(f"'{name}' has been added to the cart. ")
      items.append(name)
      prices.append(float(cost) * int(quantity))

# 2. option is to show cart
    elif option == '2':
       print()
       print('The content of the shopping cart are:')
      
       for i in  range(len(items)):
       
            name = items[i]
            cost = prices[i]
   
            print(f'{i+1}.{name} - ${cost}')
# 3. option is to remove (pop) an item
# it only shows one item and befor that it poped the wrong item (when i press 2 it poped the 3. item)
    elif option == '3':
       print()
       print('The content of the shopping cart are:')
      
       for i in  range(len(items)):
       
            name = items[i]
            cost = prices[i]
   
            print(f'{i+1}.{name} - ${cost}')
            
       pop_index = int(input('Which item would you like to remove? ')) 
       if index == pop_index: 
      
               items.pop(int(pop_index))
               break

       print('Item removed.')
                  
       # how can i make it that if user tips in an invalit number that this print comes and he has to but in a new number       
       print('sorry, that is not a valid item number.')
        

# 4. option is to show total
    elif option == '4':
      running_total = 0
      
      for total in prices:
        running_total += float(total)
       
      print(f'The total price of the items in the shopping cart is: {running_total:.2f}')
       


# 5. option is to stop the progress
    elif option == '5':
     print('Thank you. Goodbye.')
     #name != 'quit'
     break
     
There are more errors but, the 1st to stand out to me is trying to remove items from a list by entering a number. The number entered will not match the list index. List index starts at 0. You can remove the if statement and the item will be removed as long as it's not the last item as it does not have the index number entered.
Thx. And sorry for the mess. I m new to a Lot of Things. I m Not Sure If i understood you right so i should Tell them to but in the Item and Not a number to Pop and how would the Code Look Like ?

Thx for all of your patients
Here is one approach to think about. Note: I have done very little error checking.

items = {}

def additem():
    ''' Function for adding items '''
    item = input('What item would you like to add?\n>>  ')
    
    cost = input('What is the price of item?\n>> ')

    howmany = input(f'How many {item}\'s would you like?\n>> ')

    total = int(howmany) * float(cost)

    items[item] = (cost, howmany, total)
    print(f'{howmany} {item}\'s have been added to cart @ ${cost} each.')
    print()

def deleteitem():
    ''' Function for deleting items '''
    for index, item in enumerate(items):
        print(f'{index}. {item}')

    which = input('Remove which item?\n>> ')

    if int(which) == index:
        del items[item]
        print(f'{item} has been removed from cart.')
    else:
        print('An error has occurred')

def gettotal():
    ''' Function for getting total '''
    total = 0
    for key, value in items.items():
        print(f'{value[1]} {key} @ {value[2]}')
        total += value[2]
    print(f'Total: ${total}')
    

def view():
    ''' Function for viewing items '''
    print('Shopping Cart:')
    if len(items) > 0:
        for item, cost in items.items():
            print(f'{cost[1]} {item}\'s ${cost[2]:.2f}')
    else:
        print('Shopping cart is empty.')

while True:
    print()
    print('Select one of the following options')
    print('1. Add Item\n2. View Cart\n3. Remove Item\n4. Compute  Total\n5. Quit')
    print()
    option = input('>> ')
    try:
        option = int(option)
        match option:
            case 1:
                additem()
                
            case 2:
                view()

            case 3:
                deleteitem()

            case 4:
                gettotal()

            case 5:
                print('Goodbye!')
                break

            case _:
                print(f'{option} is not a valid option.')
    except ValueError:
        print('Error! Please enter whole numbers only.')
    print()