12.srtings in lists in functions!help!


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/3/5?curriculum_id=4f89dab3d788890003000096#


I expected it to search through the words and add them to result


def join_strings(words):
    result = ""
    for items in words:
        result.append(items)
        return result


#2

the function is supposed to take a list as an input and result in a string as a result.

in your loop, you used result.append(items) which is a method to append items into a list. this will cause your returned "result" to be a list instead of the desired string.

so in this case you should just use result += items within the loop then the result would be a string.


#3

@snip3rking,
Python uses identation to mark so-called code-blocks
For instance
Using

words="These words"
for item in words:
    # indentation to mark the FOR-loop code-block
    # at every iteration =item= will get 1 character assigned
    # if you now use a print-Statement
    # you will print the assigned character in this iteration
    print item
#as you now have no identation
#you have indicated the end-of-code-block
#and the print-statement will display the last =item= VARIABLE value
print item

===============================================

If you define a function
and then use the return statement
a result is returnd AND the function is immediately EXITED
As you placed the return statement in the FOR-loop code-block
the FOR-loop will exit after the 1st iteration AND the function will be EXITED......!!!

===============================================

If you want to divide a sentence into words,
you will have to use the space-character as a separator
and use a build-in function, to break the sentence into words
words.split()

sentence="this is a sentence"
def join_strings(line_of_words):
    words= line_of_words.split()
    #you initialize =result= as an empty =string=
    result = ""
    for item in words:
        #result = result + item
        #or in short
        result += item
    return result
    
print join_strings(sentence)

==================================

sentence="this is a sentence"
def join_strings(line_of_words):
    #you initialize =result= as an empty =string=
    result = ""
    for item in line_of_words:
        #space character should be skipped
        if item != " ":
            # item contains character for character
            #result = result + item
            #or in short
            result += item
    return result
    
print join_strings(sentence)

===============================================

== guidance ==
https://docs.python.org/2/reference/
http://legacy.python.org/dev/peps/pep-0008/
http://docs.python.org/2/reference/expressions.html
http://docs.python.org/2/library/functions.html
https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences
http://stackoverflow.com/questions/101268/hidden-features-of-python?rq=1


https://en.wikipedia.org/wiki/Python_(programming_language)


#4

@snip3rking,

the FUNCTION talk

def myFunc( param1, param2):
    # Begin of =myFunc= FUNCTION-BODY
    # this =myFunc= function- has 2 PARAMETERS param1 and param2
    # param1 and param2 PARAMETERS are used 
    # as -local- VARIABLES throughout the =myFunc= FUNCTION-BODY
    print( param1 + " and " + param2 )
    #End of =myFunc= FUNCTION-BODY

If you want to call/execute the myFunc function
you will have to add a pair of parentheses to myFunc
like
myFunc()
As the myFunc function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren")

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++ function with 1 parameter using return-statement

def myFunction( param1 ):
    # //Begin of =myFunction= FUNCTION-BODY
    # //=myFunction= function has 1 PARAMETER param1
    # //this param1 PARAMETER is used as a -local- VARIABLE
    # //throughout the =myFunction= FUNCTION-BODY
    return param1;
    # //End of FUNCTION-BODY

You have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the =myFunction= FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 )

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the print-method to do a display.

theResult = myFunction( 4 )
print theResult

OR directly

print myFunction( 4 )

#5

done this and it has only come up with 1 word from the list


#6

@snip3rking
Well, that sounds like you
still having the return statement in the same indentation-level as the FOR-loop code-block.

A return statement, causes the FOR-loop to end after the 1st iteration
AND the function is exited !!!