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

nicholas Christie
nicholas Christie
433 Points

what's wrong?

?

trial.py
def add(always,win):
try:
    add=int(input("Give me a argument: "))
    except ValueError:
    return(None)
     first_num= float (always)
     second_num= float(win) 
else:
    return(first_num+second_num)

1 Answer

Steven Parker
Steven Parker
231,269 Points

Here are a few hints:

  • everything that is part of a function must be indented more than the "def" line
  • a "try" and its corresponding "except" (and optional "else") must all be indented the same amount
  • this function should use the arguments passed in, it won't need to "input" anything
  • to be able to detect conversion errors, the conversions must be inside the "try" block
nicholas Christie
nicholas Christie
433 Points

played around with it took out int and input wasn't really sure what to do in the try block I put try: add="always,win" return(None) first_num= float(always) second_num=float(win) else: return (first_num+second_num)

Steven Parker
Steven Parker
231,269 Points

It's hard to see what's happening there without indentation. When posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Even without formatting, this line doesn't seem to make sense :point_right: add="always,win"
Also the "except" seems to be missing.