DEV Community

loading...

How to add and remover items from a list

arthur
・1 min read

Iam learning how to use lists in pyhton, but iam having problems with my code, that´s about a grocery cart.
while True:
n = input().split()
comand = input()
lista = [n]
if comand == 'show':
lista.sort()
if comand == 'add':
lista.append(input())
if comand == 'remove':
lista.pop()
if comand == 'terminate':
break
print(lista)

show == show itemns in ascending order
add == adding a new number
remove == removing a number of the list
terminate == exit

if you cannot help thanks for just read my problem :)

Discussion (1)

Collapse
walter11 profile image
Atharv-777

n = input('Enter your choice : ').split()
lista = list(n)
while True:
comand = input('Your command : ')
if comand == 'show':
lista.sort()
if comand == 'add':
lista.append(input())
if comand == 'remove':
lista.pop()
if comand == 'terminate':
break
print(lista)

Try this code, it is working..
Actually in your code "lista" was not getting updated, so take it outside while loop.