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

Stuck on float() function

at one point this let me go to 3 but now it wont

def add(add1, add2): added = add1 + add2 float(added) return(added)

when i get to challenge 3 not only do i not understand the question but the code is completely different and the only reason i went back to challenge 2 is because the challenge 3 code is something it tried over and over and it didn't work

def add(add1, add2): return(add1 + add2) float(add1, add2)

am i suppose to make a float function before the add function or is this thing bugged out?

CH2 task: Let's make sure we're always working with floats. Convert your arguments to floats before you add them together. You can do this with the float() function.

Ch3 task cant seem to get to again....wondering how if i really did the 2nd challenge or if it bugged out somehow

trial.py
def add(add1, add2):
    added = add1 + add2
    float(added)
    return(add1 + add2)

def add(add1, add2): added = add1 + add2 float(added) return(added) is what i have no return(add1 + add2)

nm just tried return(float(add1) + float(add2) and works...still wierd i did it different way and cant recreate that way XD

2 Answers

Jacob Dillson
Jacob Dillson
2,932 Points

Challenge 2 is asking you to float the two arguments (i.e. add1 and add2) before you add them together.

def add(add1, add2):
    add1, add2 = float(add1), float(add2)
    total = add1 + add2
    return total

Also the total variable is unnecessary, I just thought that it might be a tad bit easier to read.

Challenge 3 is asking you to implement a try and except to make sure that the variables that you are dealing with are not strings. Hint: The code from challenge 2 goes into the try field...

If you have any further questions for challenge 3, reply to my comment and I will try to further explain it. Happy Coding!

Does comma act as a type of list tool? I didn't know you could assign two variables on one line or was it already declared a variable in the add funnction argument by treating the comma as part of the variable and was a conversion done instead?

Jacob Dillson
Jacob Dillson
2,932 Points

The comma is to separate what the variable is being assigned to. The variable before the first comma will be given the value of the the first comma on the opposite side of the equals sign. It is a personal preference to declare both on the same line as an act of shortening code.

    firstVariable, secondVariable = 1, 2

Example above should give you a better idea of what I mean.