I am trying to make a dictionary of foods that are good and bad for dogs

Question:

I am having a problem with having it printing out a response of each item in my list, but if I get rid of the loop responsible, I can’t check a users input, for example, if they put ‘almond’, it will say it doesn’t know, because in my list, it says ‘almonds’.

That is why I do a loop through each element, so it will search for the word in the element, but the loop prints that it can’t find the answer a few times (unnecessarily!) and then the answer.

I hope this is understandable, and here is my code:

good = ['bread','cashews','cheese','coconut']
bad = ['almonds','chocolate','cinnamon']
def check(food):
  for b,g in zip(bad,good):
    if food.lower() in g:
      print(f'Yes, dogs can eat {food}.')
      break
    elif food.lower() in b:
      print(f'No, dogs cannot eat {food}.')
      break
    else:
      print(f'{food} is not in my dictionary yet. My apologies.')

while True:
  f = str(input('What food would you like to check?: '))
  check(f)
Asked By: Jarod L

||

Answers:

you need to loop through the two lists seperately:

good = ['bread','cashews','cheese','coconut']
bad = ['almonds','chocolate','cinnamon']

def check(food):
    for g in good:
        if food.lower() in g:
            print(f'Yes, dogs can eat {food}.')
            return None
    for b in bad:
        if food.lower() in b:
            print(f'No, dogs cannot eat {food}.')
            return None
    else:
        print(f'{food} is not in my dictionary yet. My apologies.')
        return None

while True:
    f = str(input('What food would you like to check?: '))
    check(f)

adding the return statement is important so that the function stops where the condition is met, if you remove the return statements, it will always print the last line regardless. (you could also use break but i prefer return)

Answered By: Muhd Mairaj

You do not need the zip() function, and you would need a return (instead of break) to end the function. Also, I suggest to add an exit condition, so that the while loop can be stopped.

Here’s my suggestion:

good = ['bread','cashews','cheese','coconut']
bad = ['almonds','chocolate','cinnamon']

def check(food):
    if food.lower() in good:
        print(f'Yes, dogs can eat {food}.')
    elif food.lower() in bad:
        print(f'No, dogs cannot eat {food}.')
    else:
        print(f'{food} is not in my dictionary yet. My apologies.')
    return

while True:
    f = str(input('What food would you like to check?: '))
    if f == 'exit':
        break
    check(f)

Output:

What food would you like to check?: almond
almond is not in my dictionary yet. My apologies.
What food would you like to check?: almonds
No, dogs cannot eat almonds.
What food would you like to check?: exit
Answered By: perpetualstudent

You this simple pythonic approach and get rid of loop:

def check(food):
    if any(list(map(lambda g: food.lower() in g, (g for g in good)))):
        print(f'Yes, dogs can eat {food}.')
    elif any(list(map(lambda b: food.lower() in b, (b for b in bad)))):
        print(f'No, dogs cannot eat {food}.')
    else:
      print(f'{food} is not in my dictionary yet. My apologies.')
Answered By: Zain Arshad
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.