Oct-19-2024, 06:18 AM
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
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