9...using element


#1

def list_function(x):
return [1]

n = [3, 5, 7]
print list_function(n)

wat wrong with this???????


#2

What actually happend?
Because looking at your code, it seems to work, if you fix the indentation.

Edit:Found the problem, explaning on the next post.


#3

still its nt working ..


#4

Ugh, got it!
The problem is: in your function you're returing some element at position [1] but you're not telling where that element is.
You need return the value at position [1] of the list that was passed as a parameter.

Check the examples, at your left on section 9, to see how it's supposed to be.


#5

still ny get:/ plse tell properly...:frowning:


#6

Hi try it like that if its not workin try ro refresh ythe page

def list_function(x):
    return x[1]

n = [3, 5, 7]
print list_function(n)

#7

def list_function(x):
    return x[1]

You were missing that.


#8

garcias..:)..Thank you:)


#9

why does

return x[1]

work, but

return n[1]

doesn't work?


#10

HI you have a function name list_function that has as parameter x so you should use the parameter x so that when you call the function with the argument n it will return 5

print list_function(n)

Yes you could do that

n = [3, 5, 7]
def list_function():
	return n[1]

print list_function()

it will return the same thing but the lesson want you to use the other way


#11

ahhh that makes sense! Thank you!


#12

Hi @wizmarco, how do you know which brackets to use? Because I was trying it with () and it wouldn't work but when I tried with the square brackets [] it worked... :confused:


#13

HI do you mean this line?(which should be)

return x[1]

why do I know that I should use [] or () because if you did the the lesson Access by Index
they taught how to access things by index..

#A list planet
planet = ["Earth", "Mars"; "Neptune"]
#if you want to access to Mars you will do it like that
print planet[1]
#Output 
Mars

So that why it works with [] and not ()


#14

Ah, that makes sense. Thanks! :thumbsup:


#15

Hi,
In Python we have these 3 types of brackets we use while coding ==>

  • Parentheses ( ) are used to define Tuples, Function , Loops, Order of operations
    eg:- def ( ):
    for item in range(...):

      * Different ways to define a Tuple
        a_tuple = ('amy', 'bar') 
        another_tuple = 'hello world',
        empty_tupe = ( )
        not_a_tuple = ('banana')
  • Curly braces { } are used to create Dictionaries or Sets
    eg:- dict = { 'apple' : 40, 'pear' : 30 }
    dict_2 = { }
    dict_3 = {'kiwi' : 120, 'cherry' : 720}

  • Squared brackets [ ] are used to define mutable data types(where the contents can be changed) like List
    eg:- list_animals = ['panda', 'lion', 'tiger']
    empty_list = [ ]
    list_of_dictionaries = [dict, dict_2, dict_3 ]


#16

Hi,

Thank you for that lovely summary. However, I do have one question:

What's a Tuple?


#17

whitespace at return and retrun x at index 1.


#18

Your code does not show where you added 3 to the index. How did you add 3?