'Python: Assign a number to a string
I wrote a "shopping cart programm" which can add, remove, show and clear the content of the shopping cart.
Now I want to be able to remove content from the cart with an index number. For example: If I wanted to add an apple to my cart and list the content of my cart it would looks something like this: https://i.stack.imgur.com/9kFpv.png
Now I want to be able to remove a content of the cart with the indexnumber infront of the content. Im a complete noob in python so excuse me if im playing dumb.
# import necessary functions
from IPython.display import clear_output
# global list variable
cart = [ ]
# create function to add items to cart
def addItem(item):
clear_output( )
cart.append(item)
print( "{} has been added.".format(item) )
# create function to remove items from cart
def removeItem(item):
clear_output( )
try:
cart.remove(item)
print( "{} has been removed.".format(item) )
except:
print("Sorry we could not remove that item.")
# create a function to show items in cart
def showCart( ):
clear_output( )
if cart:
print("Here is your cart:")
else:
print("Your cart is empty.")
for index, item in enumerate (cart):
print( "{}) {}".format(index + 1, item) )
# create function to clear items from cart
def clearCart( ):
clear_output( )
cart.clear( )
print("Your cart is empty.")
# create main function that loops until the user quits
def main( ):
done = False
while not done:
ans = input("quit/add/remove/show/clear: ").lower( )
# base case
if ans == "quit":
print("Thanks for using our program.")
done = True
elif ans == "add":
item = input("What would you like to add? ").title( )
addItem(item)
elif ans == "remove":
showCart( )
item = input("What item would you like to remove? Type a number: ")
removeItem(item)
elif ans == "show":
showCart( )
elif ans == "clear":
clearCart( )
else:
print("Sorry that was not an option.")
main( )
This is my current code. Right now when I want to remove an item from the cart, I can only do it with writing "remove" and then the name of the item. Which looks something like this: https://i.stack.imgur.com/zi4Ez.png But I want to be able to remove an item based on the indexnumber.
Solution 1:[1]
In your removeItem(item) function, replace cart.remove(item) with cart.pop(item)
pop() removes element from list using its index number.
Solution 2:[2]
You can use the cart.pop() method. Say you'd want to remove the 3rd item in the cart, you'd use cart.pop(2) .
Solution 3:[3]
You can user the list.pop() function to take out the items,
You can also store the local item you want to remove from the list to make sure that that's the items that you want to remove:
for example:
x = list.pop(<num of item>)
Once you pop it out, it will be deleted from the list if you won't insert any integer inside the pop function, the default is to pop the last item added (FIFO)
I would suggest to work with dictionaries to organize your data much better and easier, each value can have quantity , serial number etc..
Solution 4:[4]
Here is the correct way to achieve what you wanted.
- You need to use pop() method which removes item from list by using index number. So modify your RemoveItem function like this:
def removeItem(item):
clear_output( )
try:
cart.pop(item) # Used pop instead of remove
print( "{} has been removed.".format(item))
except:
print("Sorry we could not remove that item.")
Then, In your main() function you have to convert the user input value into Integer Value so that it can be passed to a function and consumed by pop() method. Here is the updated code :
elif ans == "remove": showCart( ) item = int(input("What item would you like to remove? Type a number: "))[enter image description here][1] removeItem(item)
I tried modifying your code as per above changes and it worked very well. Please see attached output screenshot for your reference. Output
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vishwa Mittar |
| Solution 2 | Lau |
| Solution 3 | Snorko |
| Solution 4 |
