Default argument NameError

I have a question regarding the Default arguments lesson (7/11). For this code,:

def create_spreadsheet(title, row_count=1000):

      print("Creating a spreadsheet called "+ title + "with" + str(row_count) + "rows.")

create_spreadsheet("downloads", row_count)

I get the following NameError: name ‘row_count’ is not defined.
I don’t understand this, didn’t I define it as a default argument in the function definition with “1000”?

when you call the function:

create_spreadsheet("downloads", row_count)

row_count is an undefined variable.

The default argument is used when no argument provided at function call. So the call should only have one argument.

Aha I see! That makes sense. Another question relating to this exercise is:

To concatenate ‘rowcount’ to a string, it is necessary (says the assignment) to use str(row_count) in the print statement (like i did in the example).

But would it also be correct if I changed the function from this:

def create_spreadsheet(title, row_count=1000):


def create_spreadsheet(title, row_count=“1000”):

this? Since putting the row count in brackets, it reads as a string, right? If I do this, I don’t have to put str() in the print statement. Which for me seems easier.

we talked about in concatenation already in the other topic?

You do the concatenation fine:

print("Creating a spreadsheet called "+ title + "with" + str(row_count) + "rows.")

why would you want change the data type of the default argument?

Yes, we did talk about concatenation. However now I wonder what the difference is between stating a number in a print as str(), or changing the number to a string with “”"" in the parameter. But I see you already answered that: then I change the data type of the default argument. Merci :slight_smile:

by making the default argument a string:

def create_spreadsheet(title, row_count="1000"):

and now later you decide you need math with the row count:

print(row_count * 2) # gives: 10001000

you might get unexpected results.

using the right data type is very important. We temporary need a string version of the number when doing concatenation, although, there are better approaches available as discussed earlier.