python-3.x 如何检查正确的产品数量和打破我的while循环?

5vf7fwbs  于 2022-11-26  发布在  Python
关注(0)|答案(1)|浏览(118)

我得到了一个使命,写一个代码到杂货店购物清单,每一个数字,我把应该激活其他功能。我得到了3个问题,在这个代码。
1.更大的问题是当我输入数字9时循环应该中断,但这不是中断。
1.在函数how_moch_product_name_in_list中,我需要一个for循环,它将how_much_products加1,就像相同名称的产品的数量一样,它总是写1,因为如果我输入beer,循环将运行1次。例如,如何正确地更改和写它。
1.我试着把可迭代的number写为int,就像number = int(input())一样,而if语句的其余部分也写为int,他给我写了'int' object is not iterable,我需要如何把它修改为int,这样就可以了?这是完整的代码。

def print_list(grocery_list):
    """The function print grocery shopping list.
    :param my_list: Shopping list.
    :type my_list: list.
    :return: none
    :rtype: none
    """
    print(grocery_list)

def print_len_list(grocery_list):
    """The function print the legth of grocery shopping list.
    :param my_list: Shopping list.
    :type my_list: list.
    :return: none
    :rtype: none
    """
    print(len(grocery_list))

def product_in_grocery_list(grocery_list):
    """The function check if the product is in the grocery shopping list.
    :param my_list: Shopping list.
    :type my_list: list.
    :return: none
    :rtype: none
    """
    check_product_name_in_grocery_list = input("""the ____ product is in the grocery list?
    """)
    check_product_name_in_grocery_list = check_product_name_in_grocery_list.split(",")
    print(check_product_name_in_grocery_list)
    if check_product_name_in_grocery_list in grocery_list:
        print(True)
    else:
        print(False)

def how_moch_product_name_in_list(grocery_list):
    """The function check how much products there is in the grocery shopping list.
    :param my_list: Shopping list.
    :type my_list: list.
    :return: none
    :rtype: none
    """
    check_how_much_product_name_in_grocery_list = input("""how much the ____ product is in the grocery list?
    """)
    how_much_products = 0
    for check_how_much_product_name_in_grocery_list in grocery_list:
        if check_how_much_product_name_in_grocery_list in grocery_list:
            how_much_products += 1
    print(how_much_products)

def delete_product_from_list(grocery_list):
    """The function delete product from grocery shopping list.
    :param my_list: Shopping list.
    :type my_list: list.
    :return: none.
    :rtype: none.
    """
    delete_product = input("""delete ___ product from grocery list
    """)
    grocery_list.remove(delete_product)
    print(grocery_list)

def add_product_to_list(grocery_list):
    """The function add product to grocery shopping list.
    :param my_list: Shopping list.
    :type my_list: list.
    :return: none.
    :rtype: none.
    """
    add_product = input("""add ___ product to grocery list
    """)
    grocery_list.append(add_product)
    print(grocery_list)

def print_ilegal_products(grocery_list):
    """The function check if prodoct is liegal product in the grocery shopping list.
    :param my_list: Shopping list.
    :type my_list: list.
    :return: none
    :rtype: none
    """
    ilegal_products = []
    for j in grocery_list:
        if j.isalpha() == False:
            ilegal_products.append(j)
        elif len(j) < 3:
            ilegal_products.append(j)
    print(ilegal_products)

def remove_duplicates_from_list(grocery_list):
    """The function check if there is duplicates products is in the grocery shopping list and removing them.
    :param my_list: Shopping list.
    :type my_list: list.
    :return: none.
    :rtype: none.
    """
    grocery_list = list(dict.fromkeys(grocery_list))
    print(grocery_list)

def process_grocery_store(grocery_list):
    """The function processing the grocery shopping list.
    you put number and this number is the action you want to process the list and it run the right function in a loop until you input 9 for a number and then the loop breaks.
    :param my_list: Shopping list.
    :type my_list: list.
    :return: none
    :rtype: none
    """
    number = input()
    for i in number:
        if i == "1":
            print_list(grocery_list)
        elif i == "2":
            print_len_list(grocery_list)
        elif i == "3":
            product_in_grocery_list(grocery_list)
        elif i == "4":
            how_moch_product_name_in_list(grocery_list)
        elif i == "5":
            delete_product_from_list(grocery_list)
        elif i == "6":
            add_product_to_list(grocery_list)
        elif i == "7":
            print_ilegal_products(grocery_list)
        elif i == "8":
            remove_duplicates_from_list(grocery_list)
        elif i == "9":                
            return "break"

def main():
    #grocery_store_list = input("""enter your grocery store list here
    #""")
    grocery_store_list = ['cucamber', 'cheese', 'egg', 'soap', 'tomato', 'chips', 'fish', 'meat', 'soymilk', 'chocklate', 'coffee', 'lettece', 'orange', 
    'apple', 'salmon', 'bread', 'beer', 'beer']
    #grocery_store_list = grocery_store_list.split(",")
    while process_grocery_store(grocery_store_list) != "break":
        process_grocery_store(grocery_store_list)
        

if __name__ == "__main__":
    main()
35g0bw71

35g0bw711#

main函数中的while循环调用process_grocery_store函数两次-一次在条件求值时调用,一次在条件为True时调用,并且忽略那里的返回值。因此,如果在循环内运行函数时输入“break”,则“break”没有任何作用。
请尝试以下操作:

status = ""
while status != "break":
    status = process_grocery_store(grocery_store_list)

对于第二个问题,在for循环中重写input()的值,这样循环中的条件总是满足的,并且总是增加how_much_products。另外,不是检查迭代的项是否是你想要的,而是检查它是否在列表中。

wanted_product = input("""how much the ____ product is in the grocery list?
""")
how_much_products = 0
for product in grocery_list:
    if product == wanted_product :
        how_much_products += 1
print(how_much_products)

相关问题