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

Amayae Williams
PLUS
Amayae Williams
Courses Plus Student 711 Points

Task 3 challenges

I was able to get the desired result in workspaces, but when answering the third task for the question. Am I missing something here?

def add(num1, num2): try: num1 = int(input("Give me a number ")) num2 = int(input("Give me another number ")) except ValueError: return("None") else: return(float(num1) + float(num2)) answer = add(9,9) print(answer)

trial.py
def add(num1, num2):
    try:
        num1 = int(input("Give me a number "))
        num2 = int(input("Give me another number "))
    except ValueError:
        return("None")
    else:
        return(float(num1) + float(num2))
answer = add(9,9)
print(answer)

2 Answers

Hi !

They wanted you to use the try/except blocks or the if/else blocks but not both at the same time, because as you can see there is no if in your code, then you can't just add a else, here is a simple solution to your problem :

def add(num1, num2):
    try:
        return float(num1) + float(num2)
    except ValueError:
        return None
Youssef Moustahib
Youssef Moustahib
7,779 Points

Hmm I think you have overcomplicated it, here you go:

def add(x,y): try: a = float(x) b = float(y) except ValueError: return None else: return a + b