FAQ: Introduction to Functions - Review

The message you are getting explains what the error is and what Codecademy expects as output. Did you make sure to put a space, like so: "Row "?

If you did put a space, please post your code here (formatted using the </> button) so that those trying to help can see the problem.

Welcome to the forums!

Can someone Please help me I’m stuck!!! part 3 of the exercise
https://www.codecademy.com/courses/learn-python-3/lessons/intro-to-functions/exercises/review

What have you written to this point? Please show us your code.

Hi. I also had an issue arise when completing this. The part that threw me off was when the lesson previously stated that "when an input, (def function_name(input, input)), has a default value, it would be considered invalid to use a positional argument on that input. For example:

FUNCTION WHERE DEFAULT VALUE IS USED:

def my_new_function(my_favorite_animal, amount_they_eat=1)
print(“I feed my " + str(my_favorite_animal) + " " + str(food_they_eat) + " can of tuna each day.”

What the lesson says should not be done:
my_new_function(cat, 1)
lesson said that this method was invalid

What the lesson says should be done, and what the result should be:
my_new_function(cat)

the result:
“I feed my cat 1 can of tuna each day.”

In the review, however, the value for num_repeats in the defined function, was given a default value i.e “num_repeats=10”
Then later, they put in the code, for num_repeats , lyrics = repeat_stuff("Row ", 3), 3 supposedly being a positional argument, which was not supposed to be allowed for an input such as num_repeats, which was given a default value of 10.
Therefore, I would like to understand why this was done, because following the directions got me to the result of not being able to properly answer question 6.

Also, you can find the information I was referring to on the page entitled, “Keyword Arguments”. I would really appreciate some clarification on this. Thank you

That does seem like a dubious explanation. We are fully permitted to supply an argument to a position where there is a default value set. Our argument will replace that one.

If we know the default is, say, 1 then it makes little sense to use that as an argument, but it is valid, just the same. The parameter will take the value, 1, as expected.

One does not see how there is any reason to make a statement such as you mention. It follows though, if someone is reading the code and we use a positional argument that matches the default they may raise an eyebrow but I really don’t see an issue there.

It is necessary to try and understand other people’s thinking, so that you can help to readjust it. If I make a statement, it is because I am trying to understand. That is a good reason to make a statement, because I have a question :slight_smile:

Nevertheless, all I am saying is that the point in the lesson previously entitled: “Key arguments”, made a statement that made it seem as if an argument with a default value, could not later have a positional value. It might help if you look at the Key arguments lesson, and at least see what I was reading. I would likely benefit from maybe a different explanation of that lesson, because I likely misunderstood what it was saying, if what you have said is true about the value being replaced whether there is a default or not.

Thank you for your timely response. (‘v’)

1 Like

FTR, it was not your statement in particular, but the one in the narrative to which you referred.

Please post a link to the exercise in question so we can follow up on your request.

Hi. Thanks for responding so fast. Lol. My brother explained something about how a parameter, even though it has a default value, can be overwritten. I didn’t know that previously, and it all makes sense now. Nevertheless, here’s the link to the article:

https://www.codecademy.com/paths/build-chatbots-with-python/tracks/introduction-to-python-and-chatbots/modules/learn-python3-functions/lessons/intro-to-functions/exercises/keyword-arguments

1 Like

Once you give an argument a default value (making it a keyword argument), no arguments that follow can be used positionally.

Okay, I can see where this could be confusing. There is no error in the statement, though. The author is telling us that we cannot have any positional arguments after defaults.

def quadratic(a=1, b=0, c=0, x):
    return a * x ** 2 + b * x + c

The above is invalid because x is a positional argument. It can not be placed after any default parameters and will likely raise a syntax error, or possibly a run time error (exception).

>>> def quadratic(x, a=1, b=0, c=0):
    return a * x ** 2 + b * x + c

>>> quadratic(4)
16
>>> 

Note where we are writing the x parameter… Before the defaults.

1 Like

Ok. lol I’m just glad someone understands why I got confused. But that is a lot more clearer. Thank you

1 Like

In step 3:
Change the print statement inside repeat_stuff to a return statement instead.

It should return stuff*num_repeats .

Note: Multiplying a string just makes a new string with the old one repeated! For example:

"na"*6

results in the string "nananananana" .

I can not finish it, this my code:

def repeat_stuff(stuff, num_repeats)

 return stuff*num_repeats

repeat_stuff("Row ", 3)

What I need to do next? Thank you for your help!

why not?

When I run your code, I get a syntax error:

  File "script.py", line 1
    def repeat_stuff(stuff, num_repeats)
                                       ^
SyntaxError: invalid syntax

What do you think causes this error?

I don’t know why line 1 is Syntax Error :frowning: because in step 1 and 2 it ok

Ah sorry I found it :(( it a basic syntax

I’ve finished project “Getting Ready for Physics Class”, below is my code:

train_mass = 22680

train_acceleration = 10

train_distance = 100

bomb_mass = 1

def f_to_c(f_temp=100):

  return (f_temp-32)*5/9

  f100_in_celsius = f_to_c

def c_to_f(c_temp=0):

 return (c_temp)*9/5 + 32 

 c0_in_fahrenheit = c_to_f

def get_force(mass, acceleration):

 return mass * acceleration

mass = 22680

acceleration = 10

train_force = get_force(mass, acceleration)

print("The GE train supplies " + str(train_force) + " Newtons of force.")

def get_energy(mass, c):

 return mass * c**2

bomb_energy = get_energy(1, 3*10**8)

print("A 1kg bomb supplies "+ str(bomb_energy) + " Joules.")

def get_work(mass, acceleration, distance):

 return mass * acceleration * distance

mass = train_mass

distance = train_distance

acceleration = train_acceleration

train_work = get_work(train_mass, train_distance, train_acceleration)

print("The GE train does " + str(train_work) + " Joules  of work over " + str(train_distance) + " meters")

And the result:

The GE train supplies 226800 Newtons of force.
A 1kg bomb supplies 90000000000000000 Joules.
The GE train does 22680000 Joules  of work over 100 meters

Could someone help me to review this work? Thank you so much!