How Saturday started
Like everyday I have big plans for learning on the weekend.
Like most Fridays I stay up late watching youtube videos..
The Result = waking up late and having little motivation!! Does anyone else sympathize?
Python
My plan for today was to learn some python. I have a long hate relationship with coding, so python is my way to force it into a friendship
I have been going through www.learnpython.org, which is a great free interactive python tutorial websites. Lots of tutorials great for beginners like me!!!
So I have been plowing along, and come to the functions section. All nice and easy, but wait functions within functions and then a 3rd function call the prior 2, of which the 2nd would add a string onto the result of the first function. Complicated enough for you? Don't worry I was like "draw drop".
Anyhow good news I passed it... After being like wtfomgbbq.
The exercise was:
Exercise
In this exercise you'll use an existing function, and while adding your own to create a fully functional program.-
Add a function named list_benefits()- that returns the following list
of strings: "More organized code", "More readable code", "Easier code
reuse", "Allowing programmers to share and connect code together"
-
Add a function named build_sentence(info) which receives a single
argument containing a string and returns a sentence starting with the
given string and ending with the string " is a benefit of functions!"
-
Run and see all the functions work together!
self note: OMG the copy paste works amazing in BACKTRACK
# Modify this function to return a list of strings as defined above
def list_benefits():
pass
# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit):
pass
def name_the_benefits_of_functions():
list_of_benefits = list_benefits()
for benefit in list_of_benefits:
print build_sentence(benefit)
name_the_benefits_of_functions()
in which the answer was
# Modify this function to return a list of strings as defined above
def list_benefits():
return ("More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together")
# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit):
return benefit +" is a benefit of functions!"
def name_the_benefits_of_functions():
list_of_benefits = list_benefits()
for benefit in list_of_benefits:
print build_sentence(benefit)
name_the_benefits_of_functions()
Now the red + sign was the hardest part. Like the answer looks obvious and bloody easy.
I originally had the function
# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit):
return benefit ," is a benefit of functions!"
Now having a "," instead of a + gives a massively different ouput.
Instead of the first function string having the 2nd function string concatenated together which you think would happen. It turns into a bloody tuple!! As a beginner I was stunned, that was the omgwtfbbq moment, over and over I could only get the tuple printed out, it would include both strings with brackets around them with single quotes. So a tuple of the objects, instead of the objects. Not sure if I used the correct terminology.
So you can see the difference. Which seems quite obvious, but to a begginer the trials and tribulations to understand it was not obvious. I asked a good friend on Facebook for advice, he is much more skilled and established than I.
Hopefully this post helps other noobs to understand that many simple things will make you *facepalm* yourself when you figure it out...
Cheers Haydn
No comments:
Post a Comment