Home > Blockchain >  My function is terminating early - How to fix this?
My function is terminating early - How to fix this?

Time:04-11

What changes do I need to make to correct the logic and to allow it to run through the entire function?

I am trying to call a function but each time the function gets to the line where the user enters either 'all' or 'category' it stops working. What I am trying to do is to allow the user to enter either all or category and to have the program either display all recipes listed within the dictionary or for the program to only display recipes that meet the specified criteria (the category). Would someone be able to help point out where my codes logic is incorrect?

Please note that I am trying to call the view choice function and each time I enter either all or category it stops running, I just added the others so you can see what is being called.

def display_recipes(self):
    """Iterates through a dictionary and neatly displays its contents"""
    for number in self.recipes:
        print(f"\n{number}")
        for key, value in self.recipes[number].items():
            print(f"\t{key} : {value}")

def display_recipe_names(self):
    """Display each recipe and its cooresponding number"""
    for number in self.recipes:
        for key, value in self.recipes[number].items():
            print (f"{number} : {self.recipes[number]['Recipie Name']}")

def view_choice(self):
    """Determine if the user wants to view all recipes or only recipes of a
    selected category."""

    decision = input("Would you like to view all recipes or would you like to "
                     "view recipes of a selected category? (all or category):  ")
    if decision.lower == 'all':
        self.display_recipes()
    elif decision.lower == 'category':
        print("Please select a category from the list below: ")
        self.print_categories()
        category_selection = input("Please select a category: ")
        for number in self.recipes:
            for key, value in self.recipes[number].items():
                if category_selection.title() == self.recipes[number]['Category']:
                    print(f"{number}: {self.recipes[number]['Recipie Name']}")
                    break

def print_categories(self):
    """printing the list of unique catagories within the recipe book"""
    for category in self.categories:
        print(f"{category}")
    print("")

CodePudding user response:

The problem you're having is that the decision.lower == 'all' branch is not triggering when you enter all.

You can use print debugging to help solve the mystery. Temporarily add something like:

print("User entered string that should be equal to 'all':")
print(decision.lower)

This outputs:

User entered string that should be equal to 'all':
<built-in method lower of str object at 0x7f02afa259f0>

Welp, you're comparing a method to a string. That's why it doesn't match. You can then change it to run the method, print(decision.lower()), and verify that it's correct.

Now you can modify the branch to if decision.lower() == 'all': and try again

  • Related