Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Logic in Python Try and Except

Salman Badshah
Salman Badshah
1,473 Points

I have questions regarding the challenge tasks. (Try and Except) - Python Basics.

Q1) Task 1 asks me to create a function named 'add' which should take in two arguments, add them together and then return the total so, this is what I did. def add(num1, num2): total = num1 + num2 return (total) add(2, 2)

  • This code works fine so I go to the next task where I am asked to convert the arguments to float before adding them. How do I complete the second task? Should it be something of this sort?

def add(num1, num2): float (num1) float (num2) total = num1 + num2 return (total) add(2, 2) This method completes the criteria of converting the arguments before adding them. But this does not seem to work?

Q2) In the video titled "Fully Functional" time: "3:03" Kenneth calls the function like this

def hows_the_parrot(): print("He's pinning the fjords!") hows_the_parrot() // Called the function.

  • Now when I went to the questions section of the (The exception to the Rule) student named Tony Pitkin has done the problem in this way.

def add(num1, num2): return float(num1) + float(num2)

  • There are two problems with this answer that I don't seem to understand. He is not calling the function, in the end, the way Kenneth has explained? And he is converting the arguments while he is adding them? This does not fulfill the question criteria? What is the right way to go about this problem?

1 Answer

float() function returns a value after converting the given argument into float.

so you could use something like:

num1 = float(num1)

Just for reference:

def add(num1, num2): 
    return float (num1) + float (num2) 

So when you do

return float (num1) + float (num2) 

What it does is,

return <everything that is here>

In <everything that is here>, we call a function called float(). Once float function is called, it returns a value. now we have something like,

return <some_float_value> + float(num2)

it tries to add a number to a float() function, which returns a value. So it calls the float function and replace the function call with a value.

Finally, you have something like,

return <some_float_value> + <other_float_value>

It add the both, and return the sum. (and in above answer, by 'it', i meant compiler).

Just in case if you were wondering, enclose the code in 3 backtics (`) to add the code. See Markdown Cheatsheet for more details.