Function Intermediate: Chapter 5

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

My Code:
def open_dataset(file_name=‘AppleStore.csv’, no_header = True):
opened_file = open(file_name)
from csv import reader
read_file = reader(opened_file)
data = list(read_file)
if no_header:
return data
else:
return data[1:]

apps_data = open_dataset()

I gave appropriate indentation in my code, unlike what it is above.
What I expected to happen:
I thought the no_header = True parameter used above should should yield the same result when the code is modified accordingly.

What actually happened:
However, it yielded the result with the header.

Can you please help me understand why my code didn’t yield the correct result?

Thanks!

Hello @souhasini.madhu, welcome to the community!

To use this modified code, you have to set the no_header parameter as False when calling the open_dataset function. as in
apps_data = open_dataset(no_header = False).

This indicates that the dataset has a header row since the no_header parameter is False and as such the line of code under the else statement will be executed.

Please note that the line of code under the if statement will ONLY be executed when the condition beside it evaluates to be True.

I hope this helps.

1 Like

Thank you for your response.

I am still not clear why I cannot set no_header = TRUE in the parameter section.

Is it because of a default/fixed criterion?

Thanks.

@souhasini.madhu,

You can set no_header as True in the parameter section here when defining the function;

but you have to set the no_header as False when calling the function since the dataset 'AppleStore.csv' you are working on has a header:

Something like this. apps_data = open_dataset(no_header = False) ,

This way, you would yield the correct results.

1 Like

@souhasini.madhu

From your code:
if no_header:
return data
else:
return data[1:]

means: no_header condition is true
But your code return data
returns header with a row.

I hope this makes sense

1 Like