Betty's Bakery Question


I am working on the Betty’s Bakery assignment and in question 6 they ask us to find all recipes with one egg and to use a logical statement to get True or False for each value of eggs. However, in the video tutorial it just finds and prints the recipes that contain one egg and doesn’t use any True/False statement.

Am I interpreting the question incorrectly or is the video missing something? Maybe as a non native speaker I am missing something?

I tried to get a list with True or False, but was only able to do it for a single row in my list using a lambda function:

recipes = np.genfromtxt(“recipes.csv”, delimiter = “,”)
eggs = recipes[:,2]
one_egg = lambda eggs: “True” if eggs == 1 else “False”

If you have a boolean, it would be redundant to test whether it’s true and then return true and otherwise return false
Use the boolean directly instead.
Also, you’re using strings, not booleans.

Keep in mind that a lambda function is just a function, there’s really no such thing as a lambda function, it’s just a way you can define a function, does nothing special, only affects how you type when writing it.

def one_egg(eggs):
    return eggs == 1

Your data probably contains more than just an egg count, so you’d need to dig the egg count out of it:

def one_egg(recipe):
    return recipe.get('eggs', 0) == 1

You would probably not write it exactly like that, depends on what type a recipe has and how it lets you look things up in it.

But there was the possibility to have 1, 2 or 3 eggs so then it is not a boolean right? Also I tried to see if using strings instead of just a boolean statement was a solution, so I did write the true and false as booleans first.

Would your function print a list that gives a boolean statement where 1 egg is true and any other amount is false? or would you need to add extra lines that write it as a boolean statement.

Sorry, I am still really new to coding and all the terms are not all correctly stored up there.

what does == return?
after that operation, you have a boolean. what do you do after that? you do an extra operation where you test if it’s one thing or another, and regardless of which it is, you return the value that you already had, making this test do absolutely nothing useful

strings do not represent truthfulness, the text true/false has no special meaning. it’s text.

my functions do nothing with lists. look at the operations in them. no list things. not going to happen.
but you can apply a function to the things in a list to get a list of the results

def add1(x):
    return x + 1

stuff = [1, 2, 3]
results = list(map(add1, stuff))  # [2, 3, 4]

It took a while but I get what you mean

one_egg = lambda eggs: True if eggs == 1 else False

Is just an elaborate version of:
one_egg = lambda eggs: eggs == 1

However if you want to print one_egg now it gives me the location in the storage not a list of what is false or true. I tried to use list() but that doesn’t work. How can I fix that?

Also in the example they used:

one_egg = recipes[(eggs == 1)]

which prints this:
[[ 1. 0.125 1. 1. 0.125]
[ 2.75 1.5 1. 0. 1. ]]

However the question is, Use a logical statement to get True or False for each value of eggs . So am I misinterpreting the question or is that not the correct answer.

Sorry if I keep asking the same thing and just don’t understand your answer.

Are you trying to filter so that you only have the ones with one egg, or are you trying to get a list of true/false [True, False, True, True, ...]

For filtering it looks like you have that already figured out. If you want a list of booleans (do you, though?) then you could map your predicate over the amounts of eggs

If the objective is to get the rows that satisfy the condition, then you’d want filtering.

The solution provided by codeacademy indeed filters out the ones that satisfy the “contain one egg” condition, which I understand and it makes sense, but the question seems to ask for a list of booleans for the list of eggs, with the clause being that it contain one egg. I assume it is possible to make that list of true/false statements although it is probably quite unnecessary for most problems?

Thank you for all the help and patience.

You can’t put statements in a list. A list can refer to values. True and False are values, so you can put them in a list, but a statement like a for-loop is not something you can put in a list.

If you have a function that accepts a recipe or row whichever you plan to send to the function, then you could map that over all of them which would get you an iterable of results which you could collect into a list. This is what my example above does.

there are some original values there, a function that turns such a value into a result, this function is applied to each original value to obtain a list of results. it’s the same thing as you’re describing

Ah okey, thanks. I haven’t seen the map function explained yet in one of the excercises but I understand the idea of it by what you describe. I will definitely come back to this and see if with more information I can solve this completely by myself when I have learned more.

Once again thank you for the help.

A simplified (the built-in version can accept more arguments and probably does a bunch of sanity checking) version of map:

def map(f, xs):
    for x in xs:
        yield f(x)

I have just come to this, and I had the same question - I interpreted the task the same as the original poster and the video walkthrough didn’t seem right. There was an earlier task that used True/False in an array so I knew it was possible.

import numpy as np cupcakes = np.array([2, 0.75, 2, 1, 0.5]) recipes = np.genfromtxt('recipes.csv', delimiter=',') print(recipes) eggs = recipes[:,2] print(eggs) one_egg = eggs == 1 print(one_egg)