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 trialellie adam
26,377 PointsQ1 of 3
Hi, How to solve this add function? Q. I need you to make a new function, add. add should take two arguments, add them together, and return the total.
def add(x, y):
sum = x + y
total = 'The sum of {} and {} is {}.'.format(x, y, sum)
add(total)
thx
def add(x, y):
sum = x + y
total = 'The sum of {} and {} is {}.'.format(x, y, sum)
add(total)
8 Answers
Ryan Ruscett
23,309 PointsHey,
You are on the right track, but let's break it down.
- Function called add.
- add takes 2 arguments
- return the two added together.
I totally understand why you created a sum that was the two values added together. That is fine too. You can definitely do that. But you also made a total which is a formatted string. Which you didn't have to do. Plus you only had two {} and three values. x, y and sum. So let's just scrap the total all together.
What you are actually doing is recursion. You created a recursive function. The last line add(total). Means you are calling add again, but passing it in total. Which is a string. Which is only 1 argument. The function takes 2 and since it's not a partial or an apply type function. Well, let's just scrap that too.
let's just focus on exactly what the question is asking. I think you may have over thought it.
def add(x, y): # number 1 and 2
return x + y # number 3
Does that make sense? If not let me know and I can try to explain it differently.
Ryan Ruscett
23,309 PointsHello,
There is no need for an else statement. The below code works just fine. The only thing I would use with a try and except would be a finally if I needed it. The code below works just fine.
def add(x, y):
try:
a = float(x)
b = float(y)
except ValueError
return None
return a + b
Patric Daniel Pförtner
1,542 PointsHi Ellie,
You are very close to the answer, here is how I did it:
def add(num1, num2):
return(num1 + num2) # num 3
Why are you learning Python? Maybe we have the same aims! I am learning it to bring my Start UP www.wolf-gate.com to the next level. Thank´s to Kenneth Love it´s easily possible :)
akhter ali
15,778 PointsYou don't need to have "try/else" if you return None in one of the conditions. The following makes more sense to me.
def add(x, y)
try:
a = float(x)
b = float(y)
except ValueError:
return None
return (a + b)
ellie adam
26,377 Pointsthanks, Need some help on Q3-3. It has error msg.
def add(x, y)
try:
a = float(x)
b = float(y)
except ValueError:
return None
else:
return True
naiara gonzalez
16,778 Points def add(x, y): # <-- changed from x+y
try:
a = float(x)
b = float(y)
except ValueError:
return None
else:
return a + b
Silje Tellefsen
886 Pointsdef add(arg1, arg2):
arg1 = float(arg1)
arg2 = float(arg2)
return(arg1 + arg2)
Baur Alzhanov
7,028 PointsMy version def add(x, y): return (x + y)
Ary de Oliveira
28,298 PointsAry de Oliveira
28,298 Pointsimpressive