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

Mit Sengupta
Mit Sengupta
13,823 Points

What am I doing wrong here?

I keep getting an error that : ValueError: can not convert strings into float:'a'

here's my code:

try: float(a), float(b) except ValueError: return None else: return a + b

2 Answers

Bart Bruneel
Bart Bruneel
27,212 Points

Hello Mit,

I copy-pasted your last solution and added

def add(a,b): 

in front. With the proper indentation the test passed for me. Maybe there is a problem with the indentation?

Mit Sengupta
Mit Sengupta
13,823 Points

Hello Bart, thank you for your help. It passed. I actually did not know how I needed to have a function and but now am through.

Bart Bruneel
Bart Bruneel
27,212 Points

Hello Mit,

you need to assign the result of calling float() back to the variable. Like this

a, b=float(a), float(b)

Try for example typing the following in the python shell:

A=5
float(A)
print(A)

You will see that at the end A is still an integer and not a float. If a would have been an integer you would still return an integer in the else-block.

Happy coding!

C H
C H
6,587 Points

Mutable and immutable are some good terms to review in regards to this.

Mit Sengupta
Mit Sengupta
13,823 Points

Hello Bart,

Thanks for helping out.

Here's my modified code and it is still not passing and still getting the same error as in past:

try: a, b = float(a), float(b) except ValueError: return None else: return a + b

Can you elaborate what am I doing wrong here?