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

Sachin Kanchan
Sachin Kanchan
564 Points

Unable to do the ADD argument function program

I am unable to complete the first challenge after THE EXCEPTION TO THE RULE video tutorial. Please help

trial.py
def add():
    guess1 = int(input('Choose Entry 1: '))
    guess2 = int(input('Choose Entry 2: '))

    total = (guess1+guess2)
    print(total)
    break                   
add()

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! First, challenges tend to be very literal and you should try not to do anything the challenge doesn't specifically ask for. In this case, it's asking you to define a function that takes two arguments (or has two parameters). These are going to be numbers sent into the function by Treehouse. You are using two input statements to get two numbers, but those should have been sent in by whatever is calling the function. Also, you declared a new variable named total which wasn't asked for and you're printing out the value. But the challenge asks you to return the value. You should also not be calling the function yourself. It will be called by Treehouse where they will send two numbers of their choosing. Here was my solution for Step 1.

def add(x, y):  #this function accepts two arguments
    return x + y  #return the sum of the two numbers sent in

Hope this helps! :sparkles:

Sachin Kanchan
Sachin Kanchan
564 Points

Holy beard of Odin. I know just how easy these code challenges are. But every single time I just overthink and overestimate the difficulty, I feel like - they definitely don't just mean this. There's gotta be more. Thanks

I'll post below, what I cooked up for this coding challenge, while waiting for answers.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Sachin Kanchan I've done a lot of courses here at Treehouse and a lot of code challenges. From my experience, if you think you're overcomplicating the challenge, chances are that you're correct :smiley: The best "hints" (so to speak) are the instructions. Granted, the challenges get trickier as the difficulty increases, but I still feel like this is pretty solid advice.

Sachin Kanchan
Sachin Kanchan
564 Points

I'm trying to ensure that the user only inputs both strings or both integers.


def add():
    while True:
        user_guess1 = input('Choose Entry 1: ')
        user_guess2 = input('Choose Entry 2: ')

        if user_guess1.isalpha() and user_guess2.isdigit():
            print('You must enter similar type arguments')

        elif user_guess1.isdigit() and user_guess2.isalpha():
            print('You must enter similar type arguments')

        elif user_guess1.isalpha() and user_guess2.isalpha():
            print(user_guess1+user_guess2)
            break
        else:
            user1=int(user_guess1)
            user2=int(user_guess2)
            total = (user1+user2)
            print(total)
            break

add()

If commas, decimals or symbols are used, it'll throw a EXCEPT ValueError. But I'm unable to incorporate the EXCEPTION in the code. Please guide :) Jennifer Nordell

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Is this second set of code for the challenge or for your own personal thing you're working on?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

A ValueError exception is a built in exception type in Python. It will be invoked when a function receives an argument of the correct type but an inappropriate value. The only function you have now in your code that can produce a ValueError is the place where you are attempting to convert the strings to integers.

You can view the documentation here.

Also, your code is missing any try.