简体   繁体   English

在列表中追加用户输入

[英]Appending user input in a list

I run into a problem with the 2nd and 3rd if statement. 我遇到了第二个和第三个if语句的问题。 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. 如果按2添加产品并按1列出产品,则可以观察到我正在谈论的问题。 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 v1\n')
    print (12*tab + 'MAIN MENU \n\n')



  #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 program\n\n'
          'To choose an option type coresponding 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 wich 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 ('\n\n\n')

  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

There are 2 concern I can see: 我可以看到2个关注点:

1st, you reinitialize your shoppingList = [] within the while loop, which basically means the values in it are getting ignored every run. 首先,您在while循环中重新初始化shoppingList = [] ,这基本上意味着每次运行都会忽略其中的值。 Move this initialisation out of the while loop. 将此初始化移出while循环。

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块

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 . 在这里,您尝试删除productRm而不检查列表中是否存在productRm,这将引发ValueError I would suggest you check if the product exists and then try removing it, or contain the .remove within a try-except: 我建议您检查该产品是否存在,然后尝试将其删除,或将.remove包含在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM