5. Multiple Return Statements syntax error

Hi Sir/Ma’am, hope you are doing well. I want to know what i did wrong here, i didnt wanted to pass default argument in function, is this right and wise to do? In the final function i tried writing 5 but still got syntax error. Need help.

Screen Link:
https://app.dataquest.io/m/316/functions%3A-intermediate/5/multiple-return-statements

My Code:

def open_dataset(file_name='AppleStore.csv', apps_data):
    opened_file = open(file_name)
    from csv import reader
    read_file = reader(opened_file)
    data = list(read_file)
    
    if apps_data[0]:
        return apps_data[1:]
    
    return data

apps_data = open_dataset()

What I expected to happen:

What actually happened:

 File "<ipython-input-1-52d445e6fbc6>", line 2
    def open_dataset(file_name='AppleStore.csv', apps_data):
                    ^
SyntaxError: non-default argument follows default argument

Hello @uditchauhan You defined a function that accepts 2 parameters (1 default and 1 optional) ,

but looking at your function call you didn’t specify the default argument. Also Note that when defining a function start default argument followed by non default argument.

It would be great if you refactor your code and follow instructions keenly. Also your second argument needs a better name like header

My Code
def open_dataset(file_name, header = True):
    opened_file = open(file_name)
    from csv import reader
    read_file = reader(opened_file)
    data = list(read_file)
    if header:
        return data[1:]
    
    return data
apps_data = open_dataset('AppleStore.csv')
2 Likes

Here’s what you can do take that error and put into google which leads to this stackoverflow. Follow that example listed in the stackoverflow answer (it explains the error)

Also you specify arguments in the function definition but your function call has no arguments. @info.victoromondi’s code is the best way to do it

1 Like

i tried writing open_dataset(5) what went wrong there? Also in the code you gave you gave default arg to the header and i wanted to try without it.

the stackoverflow which you gave is too technical for me at this stage i am not able to decode it.

What it is saying is that how to organize your arguments in a function (look further down for a simpler answer in that same thread)

The point of the function is to say if there is a header go to the next row which is the data otherwise if there is no header row return data.

Also your argument (file_name) is a string, not a number. The header argument that @info.victoromondi gave is what actually you are looking for. Try the code from @info.victoromondi and see what happens

1 Like

okay i will use and revert back, but i wanted to use the code without default arguements. that was my doubt in the first place.

def open_dataset(file_name=‘AppleStore.csv’, header):
opened_file = open(file_name)
from csv import reader
read_file = reader(opened_file)
data = list(read_file)

if header:
    return data[1:]

return data

apps_data = open_dataset(header = True)

i modified my code, same error, it seems the i have to use default arguement for header in the def func right?

you need file_name to be an input argument and header to be true