Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# 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 :

In your removeItem(item) function, replace cart.remove(item) with cart.pop(item)

pop() removes element from list using its index number.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading