Appending user input in a list

Question:

I run into a problem with the 2nd and 3rd if statement. It does seem to register input but it’s not adding it to list. You can observe the problem I’m talking about if press 2 add product and press 1 for listing products. You will notice that nothing is showing up sadly. Like the list is still empty, but I have given it an item. Is there any way to fix it?

hell_is_not_frozen = True
while hell_is_not_frozen:

  #menu 
  def menu():
    tab = ' '
    print (40*'=')
    print (8*tab + 'Shopping list app v1n')
    print (12*tab + 'MAIN MENU nn')
  
  
  
  #options
  def options():
    print('[1] View products in list n'
          '[2] Add a product to list n'    
          '[3] Remove a product from list :c n'
          '[4] Exit programnn'
          'To choose an option type corresponding number:')
  
  #calling feds (defs backwords) nice joke isn't it?  :)
  menu()
  options()
  
  #Making sure input is int type
  #TODO Implement anit-other-character system 
  numberInt = raw_input()
  number = int(numberInt)
  
  #Core of this app
  shoppingList = []
  
  
  
  #checking which option was picked  
  if number == 1:
    if len(shoppingList) == 0:
      print('nYour shopping list doesn't seem to contain any products')
    print ('n')
    print('n'.join(shoppingList)) 
    print ('nnn')
  
  if number == 2:
    #taking a name of beloved product user would like to add
    productAddStr = raw_input("nWhat product would you want to add in?n")
    productAdd = str(productAddStr)
    shoppingList.append(productAdd)
    print(shoppingList)
    
  if number == 3:
    #taking a name of beloved product user would like to Remove
    productRmStr = raw_input("nWhat product would you want to add in?n")
    productRm = str(productRmStr)
    shoppingList.remove(productRm)
  
  if number == 4:
    #Exiting
    print('nSee you next time :D')
    hell_is_not_frozen = False
    
Asked By: Ezic

||

Answers:

There are 2 concern I can see:

1st, you reinitialize your shoppingList = [] within the while loop, which basically means the values in it are getting ignored every run. Move this initialisation out of the while loop.

hell_is_not_frozen = True
#Core of this app
shoppingList = []
while hell_is_not_frozen:
    # remaining code goes here

2nd is with this if block

if number == 3:
    #taking a name of beloved product user would like to Remove
    productRmStr = raw_input("nWhat product would you want to add in?n")
    productRm = str(productRmStr)
    shoppingList.remove(productRm)

Here, you try to remove the productRm without checking if it exists in the list, which will throw a ValueError. I would suggest you check if the product exists and then try removing it, or contain the .remove within a try-except:

if number == 3:
    #taking a name of beloved product user would like to Remove
    productRmStr = raw_input("nWhat product would you want to add in?n")
    productRm = str(productRmStr)
    try:
        shoppingList.remove(productRm)
    except ValueError:
        pass
Answered By: Anshul Goyal
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.