Guido's Gorgeous Lasagna code

hi all
first of all, thanks for all this site and wonderfull info inside. I’m newbie here and even for python, and here is days that i’m struggling with the lasagna exercice. i’ve found the solution, it has workend in jupyther but not here
here is my code :

def elapsed_time_in_minutes(number_of_layers, elapsed_bake_time):

PREPARATION_TIME = 2
total_preparation_time = number_of_layers * PREPARATION_TIME
total_elapsed_time = EXPECTED_BAKE_TIME - elapsed_bake_time
elapsed_time_in_minutes = total_preparation_time + total_elapsed_time

return(elapsed_time_in_minutes)

and it worked
but when i copy it here it return this error :

We received the following error when we ran your code:

ImportError: 

MISSING FUNCTION --> In your 'lasagna.py' file, we can not find or import the function named 'preparation_time_in_minutes()'. 
Did you misname or forget to define it?

please help, i don’t see what is wrong !
thanks in advance

The error says your solution is missing the function preparation_time_in_minutes(). That function needs to be defined in your solution. Try adding in that function.

Hi IsaacG
thank for reply
actually i had it in the line 3 : preparation_time_in_minutes = number_of_layers * PREPARATION_TIME
isn’t it enough ?

That’s not a function.

def preparation_time_in_minutes(number_of_layers):
PREPARATION_TIME = 2
preparation_time_in_minutes = number_of_layers * PREPARATION_TIME
return(preparation_time_in_minutes)

Thanks and sorry to come back to you , written like this with def start, isn’t a function ? do i have to add additional def function ?! sorry I’m newbie in this stuff

You may want to go back to your Hello World exercise to see how functions are written. Or start with a intro to Python tutorial.

ok i will try this way
thank you very much

hi again
this exercice is driving me exercice. here is the instructions :

Calculate remaining bake time in minutes

task 1:

  • You need to define a [function].
  • Use the [mathematical operator for subtraction]
  • This function should [return a value]
    Task 2 : Calculate remaining bake time in minutes
    You need to define a function with a single parameter representing the time elapsed so far.
    Use the mathematical operator for subtraction to subtract values.
    This function should return a value.

so i execute it as this :
EXPECTED_BAKE_TIME= 40
def bake_time_remaining(bake time elapsed):
return(EXPECTED_BAKE_TIME-bake time elapsed)
bake_time_remaining(10)

then he return that ! :

We received the following error when we ran your code:

  .usr.local.lib.python3.11.site-packages._pytest.python.py:618: in _importtestmodule
    mod = import_path(self.path, mode=importmode, root=self.config.rootpath)
.usr.local.lib.python3.11.site-packages._pytest.pathlib.py:533: in import_path
    importlib.import_module(module_name)
.usr.local.lib.python3.11.importlib.__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1204: in _gcd_import
    ???
<frozen importlib._bootstrap>:1176: in _find_and_load
    ???
<frozen importlib._bootstrap>:1147: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:690: in _load_unlocked
    ???
.usr.local.lib.python3.11.site-packages._pytest.assertion.rewrite.py:168: in exec_module
    exec(co, module.__dict__)
.mnt.exercism-iteration.lasagna_test.py:8: in <module>
    from lasagna import (EXPECTED_BAKE_TIME,
E     File ".mnt.exercism-iteration.lasagna.py", line 2
E       def bake_time_remaining(bake time elapsed):
E                                    ^^^^
E   SyntaxError: invalid syntax

please advise
thanks

Parameter names cannot contain spaces. You’ll need to use bake_time_elapsed (with underscores).

Hi all
hope doing well,
always lasagna, i’m close to remove it from my food:)
this morning i retry from first and once again had error, at the best of my knowledge, i payed attention to underscore, name…but it keep send me back error!
here is the last one
EXPECTED_BAKE_TIME=40
def bake_time_remaining_in_minutes(elapsed_bake_time):
return(EXPECTED_BAKE_TIME-elapsed_bake_time)

and it return

We received the following error when we ran your code:

ImportError: 

MISSING FUNCTION --> In your 'lasagna.py' file, we can not find or import the function named 'bake_time_remaining()'. 
Did you misname or forget to define it?

i’m not sure to understand this message, misname ? is it that the issue
to define it ? did i miss something in def function
i’m totaly lost
please help
thank you

Hi @Selena152 ,

Good work in hanging in there and reviewing your learning after sleeping on it :smile:

You are correct – you have a mis-name issue.

You’ve named your function bake_time_remaining_in_minutes(elapsed_bake_time),

But the tests are expecting to call a function named bake_time_remaining(elapsed_bake_time).

I cannot tell from your posted code if you are indenting the function body. It will need to be indented in order to run correctly:

EXPECTED_BAKE_TIME=40

def bake_time_remaining(elapsed_bake_time):
    return EXPECTED_BAKE_TIME - elapsed_bake_time

Also note that you do not need parenthesis around the return values in a function unless you are returning a tuple, or are using a complicated math formula or boolean logic that requires parens for proper evaluation.

Hope that helps – and let us know your progress!

Hi BethanyG
thanks for feedback
i correct according your advise and complete the exercice but once again i got an error
here is my response:

EXPECTED_BAKE_TIME=40

def bake_time_remaining(elapsed_bake_time):
return EXPECTED_BAKE_TIME - elapsed_bake_time
“”“return the bake time left”“”

#TODO: Define the ‘preparation_time_in_minutes()’ function below.

You might also consider using ‘PREPARATION_TIME’ here, if you have it defined.

def preparation_time_in_minutes(number_of_layers):
“”“return the preparation time taken per layer’s quantity”“”
preparation_time=2
return number_of_layers * preparation_time

#TODO: define the ‘elapsed_time_in_minutes()’ function below.

Remember to add a docstring (you can copy and then alter the one from bake_time_remaining.)

def elapsed_time_in_minutes(number_of_layers, elapsed_bake_time):
“”“returnprep time + bake time “””
return number_of_layers + elapsed_bake_time

then i had this feedback :nauseated_face:

We received the following error when we ran your code:

  .usr.local.lib.python3.11.site-packages._pytest.python.py:618: in _importtestmodule
    mod = import_path(self.path, mode=importmode, root=self.config.rootpath)
.usr.local.lib.python3.11.site-packages._pytest.pathlib.py:533: in import_path
    importlib.import_module(module_name)
.usr.local.lib.python3.11.importlib.__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1204: in _gcd_import
    ???
<frozen importlib._bootstrap>:1176: in _find_and_load
    ???
<frozen importlib._bootstrap>:1147: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:690: in _load_unlocked
    ???
.usr.local.lib.python3.11.site-packages._pytest.assertion.rewrite.py:168: in exec_module
    exec(co, module.__dict__)
.mnt.exercism-iteration.lasagna_test.py:8: in <module>
    from lasagna import (EXPECTED_BAKE_TIME,
E     File ".mnt.exercism-iteration.lasagna.py", line 16
E       """Calculate the bake time remaining.
E   IndentationError: unexpected indent

unexpected indent ?!! where
thanks in advance

Hi @Selena152 :wave:

Sadly, Python is quite picky about indentation. That includes things like docstrings and comments. Once you’ve made a function definition, everything under that definition needs to be indented to the same level, or Python throws a fit. :roll_eyes:

And for things outside a function, everything must be left-aligned to the margin (with a few exceptions you don’t need to worry about right now).

The error that you have indicates that something is not lined up indentation-wise with one or more of your functions or other lines in the file (Python parses the entire file before it evaluates, so you need to make sure everything in the file lines up). The annoying thing is that it can be a little as a single space one direction or the other – and Python will get grumpy, so it is very important to set your code editor to have consistent indentation.

I sometimes also set my editor to “show whitespace”, so that a little dot will appear for every space. It is really noisy visually, but can help with identifying where you may have had accidental leading or trailing spaces in your code.

I can’t tell by what you have pasted into your post where you have or have not indented things, so I am going to take a guess. This is what your code should look like. Remember - everything under a function definition needs to be indented at the same level. The recommendation for Python is a 4 space indent.

Additionally, docstrings should appear right under the function definition line – not at the bottom of a function. You should also remove any #TODO comment lines before submitting:

EXPECTED_BAKE_TIME=40

def bake_time_remaining(elapsed_bake_time):
    “”“Return the bake time left.”“”

    return EXPECTED_BAKE_TIME - elapsed_bake_time

def preparation_time_in_minutes(number_of_layers):
    “”“Return the preparation time taken per layer’s quantity.”“”

    preparation_time=2
    return number_of_layers * preparation_time

def elapsed_time_in_minutes(number_of_layers, elapsed_bake_time):
    “”“returnprep time + bake time “””
    
    return number_of_layers + elapsed_bake_time

Give that a go and see if the error disappears. If not, please paste your code as a markdown code block, so I can troubleshoot where the rouge indentation is happening. :smile: :wink:

Good luck - and keep going – you’re almost there!

Hi again
i had followed your advise, thank for this wise’s ones.

here is my written, i tried to have 4 identation for the second line of def ::
EXPECTED_BAKE_TIME=40
def bake_time_remaining(elapsed_bake_time):
return EXPECTED_BAKE_TIME - elapsed_bake_time

“”“return the bake time left”“”

def preparation_time_in_minutes(number_of_layers):
preparation_time=2
return number_of_layers * preparation_time

“”“return the preparation time taken per layer’s quantity”“”

def elapsed_time_in_minutes(number_of_layers,elapsed_bake_time):
return number_of_layers + elapsed_bake_time

“”“return prep time + bake time “””

i had passed the 3 first tasks but the 4th and 5th no , here is the feedback :

tasks4 ::

CODE RUN

layer_data = (1, 2, 5, 8, 11, 15)
time_data = (3, 7, 8, 4, 15, 20)
result_data = [5, 11, 18, 20, 37, 50]

for variant, (layers, time, expected) in enumerate(zip(layer_data, time_data, result_data), start=1):
    with self.subTest(f'variation #{variant}', layers=layers, time=time, expected=expected):
        actual_result = elapsed_time_in_minutes(layers, time)
        failure_msg = (f'Called elapsed_time_in_minutes({layers}, {time}). '
                       f'The function returned {actual_result}, but the tests '
                       f'expected {expected} as the elapsed time.')

        self.assertEqual(actual_result, expected, msg=failure_msg)

TEST FAILURE

One or more variations of this test failed. Details can be found under each [variant#].

tasks 5:: he show me 6 variants failure , here is one of it ::

variant 1

CODE RUN

layer_data = (1, 2, 5, 8, 11, 15)
time_data = (3, 7, 8, 4, 15, 20)
result_data = [5, 11, 18, 20, 37, 50]

for variant, (layers, time, expected) in enumerate(zip(layer_data, time_data, result_data), start=1):
    with self.subTest(f'variation #{variant}', layers=layers, time=time, expected=expected):
        actual_result = elapsed_time_in_minutes(layers, time)
        failure_msg = (f'Called elapsed_time_in_minutes({layers}, {time}). '
                       f'The function returned {actual_result}, but the tests '
                       f'expected {expected} as the elapsed time.')

        self.assertEqual(actual_result, expected, msg=failure_msg)

TEST FAILURE

AssertionError: 4 != 5 : Called elapsed_time_in_minutes(1, 3). The function returned 4, but the tests expected 5 as the elapsed time.

variant 2 ::

TEST FAILURE

AssertionError: 9 != 11 : Called elapsed_time_in_minutes(2, 7). The function returned 9, but the tests expected 11 as the elapsed time.
variant 3::
AssertionError: 13 != 18 : Called elapsed_time_in_minutes(5, 8). The function returned 13, but the tests expected 18 as the elapsed time.

and same conclusion until variant 6 , i wonder where he found these variants since i didn’t writte it !
thank you in advance

PS: i tried this code in jupyther and it ran

Allelouiyia finally i did it
thanks to all who supported me and i hope that my struggling had support others to avoid it
have nice day

2 Likes

Olá a todos, alguém poderia me ajudar por favor.

Estou no tópico 5 desse exercício no qual tem que documentar todo o código, conforme a seguir: Volte na receita, adicionando “notas” na forma de docstrings de função. Já tentei de todas as formas, busquei informações e nada.

Segue o Código:

import lasagna
EXPECTED_BAKE_TIME = 40
def bake_time_remaining(tempo=30):
    return(EXPECTED_BAKE_TIME - tempo)
def preparation_time_in_minutes(number_of_layers = 2):
    tempo_por_camada = number_of_layers * 2
    return(tempo_por_camada)
def elapsed_time_in_minutes(number_of_layers = 3, elapsed_bake_time = 20):
    """Calculate the elapsed cooking time.

    :param number_of_layers: int - the number of layers in the lasagna.
    :param elapsed_bake_time: int - elapsed cooking time.
    :return: int - total time elapsed (in minutes) preparing and cooking.

    This function takes two integers representing the number of lasagna layers and the
    time already spent baking and calculates the total elapsed minutes spent cooking the
    lasagna.
    """
    numero_do_cozimento = 2 * number_of_layers
    tempo_total = numero_do_cozimento + elapsed_bake_time
    return (tempo_total)

This exercise requires that you write docstrings for each function. Your code has a docstring for only one function. You need to add docstrings to the other two functions.

Este exercício requer que você escreva docstrings para cada função. Seu código possui uma docstring para apenas uma função. Você precisa adicionar docstrings às outras duas funções.

1 Like

Thank you very much, your help was very important!!

I managed to complete the activity. :pray:

1 Like