简体   繁体   English

Python:购物清单:不存储的商品

[英]Python: Shopping list: Items not storing

Intro: I am trying to put a shopping list code together. 简介:我正在尝试将购物清单代码放在一起。 However, every time that I input an item, instead of storing the item in the list, I get a error saying that that item is not defined. 但是,每次我输入一个项目而不是将其存储在列表中时,我都会收到一条错误消息,指出未定义该项目。

shopping_list = []


def show_help():
    print("What should we pick up at the store?")

    print("""
  Enter 'DONE' to stop adding items.
  Enter 'HELP' for this help.
  Enter 'SHOW' to see your current list.
  """)


def add_to_list():
shopping_list.append(new_item)
print("Here is the item that it's been added {}. There are now {} items".format(new_item, len(shopping_list)))


def show_list():
    index =  1
    for index, item in shopping_list:
        print("Here is the current  shopping list: {}. {}".format(index,item))
    index = index + 1
show_help()

while True:
    new_item = input("> ")

     if new_item == 'DONE':
        break
     elif new_item == 'HELP':
        show_help()
        continue
     elif new_item == 'SHOW':
        show_list()
        continue

     add_to_list()

show_list()

Question: Why is not storing the string into the list? 问题:为什么不将字符串存储到列表中?

That is true because you are not passing any item to add_to_list() method. 确实如此,因为您没有将任何项目传递给add_to_list()方法。

The code should be like: 该代码应类似于:

shopping_list = []


def show_help():
    print("What should we pick up at the store?")

    print("""
  Enter 'DONE' to stop adding items.
  Enter 'HELP' for this help.
  Enter 'SHOW' to see your current list.
  """)


def add_to_list(new_item):
    shopping_list.append(new_item)
    print("Here is the item that it's been added {}. There are now {} items".format(new_item, len(shopping_list)))


def show_list():
    index =  1
    for index, item in shopping_list:
        print("Here is the current  shopping list: {}. {}".format(index,item))
    index = index + 1
show_help()

while True:
    new_item = input("> ")

     if new_item == 'DONE':
        break
     elif new_item == 'HELP':
        show_help()
        continue
     elif new_item == 'SHOW':
        show_list()
        continue

     add_to_list(new_item)

show_list()

input() interprets the input, ie If the user eg puts in an integer value, the input function returns this integer value. input() 解释输入,即,如果用户(例如input()输入整数值,则输入函数将返回此整数值。 If the user on the other hand inputs a list, the function will return a list. 另一方面,如果用户输入列表,则该函数将返回列表。

If you don't wrap your input into quotes, Python takes your name as a variable. 如果您不将输入内容括在引号中,Python会将您的姓名作为变量。 So, the error message makes sense! 因此,错误消息是有道理的!

To avoid this error you have to cast the variable input into a string. 为避免此错误,您必须将变量输入转换为字符串。 eg: "ABC" 例如:“ ABC”


Alternatively, you can use raw_input() . 另外,您可以使用raw_input() raw_input does not interpret the input. raw_input 不解释输入。 It always returns the input of the user without changes, ie raw. 它始终返回用户的输入,而无需更改,即原始。 This raw input can be changed into the data type needed for the algorithm. 可以将原始输入更改为算法所需的数据类型。

Further Reading 进一步阅读

you also had the function show_list wrong. 您也有函数show_list错误。 This was test on Python 3.7 这是在Python 3.7上测试的

shopping_list = []

def show_help():
    print("What should we pick up at the store?")

    print("""
  Enter 'DONE' to stop adding items.
  Enter 'HELP' for this help.
  Enter 'SHOW' to see your current list.
  """)


def add_to_list():
    shopping_list.append(new_item)
    print("Here is the item that it's been added {}. There are now {} items".format(new_item, len(shopping_list)))


def show_list():
    for index, item in enumerate(shopping_list):
        print("Here is the current  shopping list: {}. {}".format(index, item))
    show_help()

while True:
    new_item = input("> ")

    if new_item == 'DONE':
        break
    elif new_item == 'HELP':
        show_help()
        continue
    elif new_item == 'SHOW':
        show_list()
        continue

    add_to_list()

show_list()

Based on your error given, I think you might be using Python 2.7 and the error is because you used input() instead of raw_input() . 根据给出的错误,我认为您可能正在使用Python 2.7,并且该错误是因为您使用了input()而不是raw_input() When using Python 3 input() should be used. 使用Python 3时,应使用input()

So change new_item = input("> ") to be new_item = raw_input("> ") and that should stop your error. 因此,将new_item = input("> ")更改为new_item = raw_input("> ") ,这应该会停止您的错误。


However there is also an error in your code where you print out the final list in the show_list function as well. 但是,您的代码中也存在一个错误,即您也在show_list函数中打印出最终列表。 A for loop only goes through each item in teh list unless you call enumerate which will return the index and the item as it loops through. for循环仅遍历列表中的每个项目,除非您调用enumerate ,它将在enumerate时返回索引和项目。

This is probably what you are looking for in that function: 这可能是您在该函数中寻找的:

def show_list():
    for index, item in enumerate(shopping_list):
        print("Here is the current shopping list: {}. {}".format(index,item))

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

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