What do I need to change to delete "number = 5"?


Where do I add function so I can delete number = 5?

Now, modify the function definition so that it has a parameter called number . Then delete the number = 5 assignment on the first line of the function.

Pass the number 1 into your function call.

We start out with,

def mult_two_add_three():
  number = 5

The parameter list is empty, and the variable is declared and defined directly in the function body.

We want to be able to supply the function with any value for number so define it in the parameter list, as the instruction suggests…

def mult_two_add_three(number):
  #number = 5

Comment out the declaration line and run the code with the arguments given in this and the next three steps.



I get the error : name ‘number’ is not defined.

My code :

def mult_two_add_three(number):
number = 1
print(number*2 + 3)

Call mult_two_add_three() here:


Error message :

Traceback (most recent call last):
File “script.py”, line 3, in
print(number*2 + 3)
NameError: name ‘number’ is not defined

So I did “view solution” : all the items were ticked green but the same screen as described above appears, except that I can go on :slight_smile:
I just want to make sure I understood the syntax and the exercise.

When a function is given a parameter, we would avoid reassigning that variable within the function since it will not have the value that was given in the call argument.

number = 1   # that line should not be there

You’ll find that it is more common to return a result than to print it in a function. Does the solution look something like this:

def mult_two_add_three(number):
    return number * 2 + 3

print (mult_two_add_three(6))    #  15

Sorry, mtf. I never saw before you had answered :frowning:
So I went back to the exercise, and yes, your answer makes sense now :slight_smile: Thanks !

1 Like