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

Except

is Except like Else?

i stumbled on a page: https://teamtreehouse.com/community/how-do-i-convert-an-argument-in-my-function-to-float-this-is-python

it helped me get through the floats, and to be honest i would have never figured it out, i was trying stuff like return(float(num1 + num2) a lot of these examples make sense on their own, but i can't quite figure out the proper way to do this one.

can someone give me a clear example?

trial.py
def add(num1, num2):
    a = float(num1)
    b = float(num2)
    return(a + b)
except ValueError:
    return None
else:
    return total

2 Answers

Brendan Chamberlain
Brendan Chamberlain
2,753 Points

In short, no except is not like else. Except blocks are used together with try blocks (and finally/else blocks optionally). When your code is in a try block, python will attempt to run that code. If an error occurs within the try block, you can manipulate the error message by using an except block. I am new myself to python so don't take my word as law; I would recommend reading the docs at www.python.org

Afloarei Andrei
Afloarei Andrei
5,163 Points
  1. Add 'try:' before 'a = float.....'
  2. Move 'return(a + b)' to 'else:'
  3. Missing indentation def...: try: .......... except: ......... else: .......