Я работаю над корзиной покупок с 5 вариантами, и мне трудно найти вариант 3, позволяющий удалить (вытащить) элемент.
Сейчас мне показывается только один элемент, даже когда я добавил в список больше элементов. мне нужно, чтобы он показывал мне все элементы, а затем я мог ввести номер одного элемента, а затем он выскакивал (не удалялся). и если я указал неверный номер, то он должен вернуться и снова попросить меня, но с правильным номером.
как вы можете определить по коду, я пробовал несколько разных вещей. я ввел код whoöe, потому что допустил ошибку вне варианта 3
#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
cost = prices
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
cost = prices
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
Подробнее здесь: https://stackoverflow.com/questions/791 ... pping-cart
Как я могу решить проблему с появлением в корзине покупок ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение