samedi 9 septembre 2017

call specific elements in a list with for-each loop, function definition and if-else-elif statements

Sorry if the title is not worded properly. First year student currently doing a unit that focuses on the concepts of Python.

How can I call specific elements (if thats the term for it) within a list with a function definition, for_each loop, function definition and if-else-elif statements.

Say this is my list:

data_sets = [
    ['0', ['1', '2', '3']],
    ['4', ['5', '6', '7']]]

And say this is my function definition:

def example(dataset):
    for element in dataset:
        if '1' in dataset:
            return 'Yeah!'
        else:
            return 'Nope!'

I do not understand why it does not return 'Yeah!' when I type data_sets[0]. I know that data_sets[0][1] does return 'Yeah!' but i want to use only one square bracket ([0]) and not two ([0][1]) to return a 'Yeah!'

example(data_sets[0]) 'Nope!'

I am meant to do an exercise that uses different numbers, etc., and it says I should be able to do that with one square bracket ([0]), but I dont know how. We aren't told what to do for the function definition, for each loop, and if-else statements, but we are given the list, and we are expected to do a similar thing with one square bracket. So that may mean I might be doing the function definition, for each loop and if-else statements wrong.

Apologies if I have used incorrect terms or jargon as I am only a beginner.

Aucun commentaire:

Enregistrer un commentaire