Home > Back-end >  how to check right number of products and breaking my while loop?
how to check right number of products and breaking my while loop?

Time:11-21

I got a mission to wright a code to grocery shopping list, every number that I put in should activete other function. I got 3 problems in this code.

  1. the greater problem is that the loop should break when I put in the number 9 and that isn't break.
  2. in the function how_moch_product_name_in_list I need a for loop that add one to how_much_products like the number of the products from the same name and it always wright 1 because the loop run over 1 time if I put in beer for example, how to chnge it and wright it right.
  3. I tried to wright the iterable number as int like number = int(input()) and the rest of the if statment also as int and he written me 'int' object is not iterable, how I need to fix this to int that work? that the full code.
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()

CodePudding user response:

The while loop in your main function calls the process_grocery_store function twice - once in the condition evaluation, and once if the condition is True, and you ignore the return value there. so if you enter "break" when the function is run the inside the loop, than the "break" has no effect.

Try something like this:

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

For the second problem, you override the value of the input() in the for loop, so the condition inside the loop is always met and you always increase how_much_products. Also, instead of checking if the item you iterate over is the one you want, you check if it is in the list.

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)
  • Related