Python 3 Coursework 7. Keyword Arguments

Hello again from me,
i am new to this programming stuff and stuck on this exercise for a few days now.
I tried everything and it just wouldn`t work as expected.

# Define create_spreadsheet():

def create_spreadsheet (title):
  print("Creating a spreadsheet called ")
  row_count = 1000

# Call create_spreadsheet() below with the required arguments:

create_spreadsheet("Downloads" + row_count)

The Code will fail to execute, with the following error statement:

Traceback (most recent call last):
File “script.py”, line 6, in
create_spreadsheet(“Downloads” + row_count)
NameError: name ‘row_count’ is not defined

That is strange, because i thought i would have defined the name ‘row_count’ as 1000.
I also tried it with the row_count parameter placed in different locations inside of the Code, but to no avail.

Help is greatly appreciated, thank`s!

If you could edit your post and format your code in preformatted text, that would be great. For now, the error seems to be coming from row_count

def create_spreadsheet (title):
  print("Creating a spreadsheet called ")
  row_count = 1000 # <= this line

it’s only been defined inside the function and cannot be called outside of that. And even if it wasn’t defined inside, it wouldn’t work as an argument, it’d throw an error. I cannot access the lesson, so I suggest you review your instructions. Also, a integer cant be added (+) to a string.

1 Like

true, but this variable has a local scope, it only exists within the function.

1 Like

Please post a link to the problem.

row_count is meant to be a parameter, not an inline variable.

def foo(title, row_count = 1000):

Here is the Link, it`s instruction 2

https://www.codecademy.com/courses/learn-python-3/lessons/intro-to-functions/exercises/keyword-arguments

As shown above by mtf, row_count is a parameter, specifically a keyword parameter. As such, it has a default value (in this case, 1000), and it can be either included or omitted within the arguments of the calling expression. If it is omitted, its default value is used in the execution of the function. If it is included, the keyword can be used with an assignment operator, or it can be omitted and the argument will be treated as a positional argument

def a(s, p = 'r', q = 'z'):
    return s + p + q

print( a('m'))   # p and q omitted
print( a('m', 'j'))  # p included as positional, q omitted
print( a('m', 'j', q = 'a'))  # p positional, q keyword

# Output:
mrz
mjz
mja

When you use the keywords, they can be in any order, but you must place all positional arguments before any that are defined using the keyword.

print( a('m', p = 'x', q = 't'))
print( a('m', q = 't', p = 'x'))
# Output
mxt
mxt
#######################
print( a('m', p = 'a', 'j'))
# Output:
Traceback (most recent call last):
  File "path\to\test.py", line 8
    print( a('m', p = 'a', 'j'))
                          ^
SyntaxError: positional argument follows keyword argument
1 Like

So it schould look like this?

def create_spreadsheet (title + row_count = 1000)

If i understand this correctly, then the row_count is in this example inside of the function.

Or do I miss something?

Within the parentheses of a function name is a sequence of parameters, separated by commas.

There are two parameters here, title and row_count. title is a positional parameter (i.e., it has no default value, and is identified by its position - in this case, first - within the parentheses.) That is established in instruction 1, which says nothing of a default value.

row_count is a keyword parameter. We know that because you are instructed to give it a default value (1000). It is analogous to the parameter p in my example above.

Ah! I see that the instruction says, " Add the parameter row_count to the function definition. Set the default value to be 1000 ." Did you take “add” to mean something resembling arithmetic addition or string concatenation? That would explain the + that you have been using! No, it simply means “put one more parameter in the function definition, the usual way, with comma separation.”

That done, the function has only one task: to print() “Creating a spreadsheet called title with row_count rows” With the appropriate numbers replacing title and row_count, like this:

def create_spreadsheet(title, row_count = 1000):
  # your code here

# Call create_spreadsheet() below with the required arguments:
create_spreadsheet("Applications", 10)

#  Expected output:
Creating a spreadsheet called Applications with 10 rows
1 Like

Yes, that little + was wrong, thank you very much everyone! Yes you were right, I misinterpreted the add as a command to use +.

I fear that I will misinterpret the Instructions quite a few Times, so be patient with me.