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 trialANOOSHA CHIKKULAPALLI
3,152 Pointsstring interpolation in methods
how to use string interpolation in methods. i think am doing the correct way..
def mod(a, b)
#write your code here
c = a%b
return "The remainder of a divided by b is #{c}"
end
5 Answers
Cena Mayo
55,236 PointsHi Anoosha,
You just need to include the actual operation within the interpolation, like so:
def mod(a, b)
#write your code here
return "The remainder of #{a} divided by #{b} is #{a % b}."
end
Notice that you need to interpolate a and b as well. So what ends up happening is that you'll call the method with some arguments, like so:
mod(4,2)
and it will return
"The remainder of 4 divided by 2 is 0."
Hope that helps!
ANOOSHA CHIKKULAPALLI
3,152 PointsUse string interpolation to return the remainder inside the sentence “The remainder of a divided by b is c.” where a is your “a” variable, b is your “b” variable, and c is the value of a % b. This is my question and the my answer was def mod(a, b) c = a%b return "The remainder of #{a} divided by #{b} is #{c}" end
Cena Mayo
55,236 PointsYou need to remove the c = a %b from your code. The challenge is asking you to perform the interpolation inside the returned sentence, not outside of it.
ANOOSHA CHIKKULAPALLI
3,152 Pointsdef mod(a, b)
#write your code here
return "The remainder of #{a} divided by #{b} is #{a%b}"
end
Is this is the correct way. If it is correct then why wouldn't it show the answer is correct.
Cena Mayo
55,236 PointsMake sure your return statement has a period at the end..
return "The remainder of #{a} divided by #{b} is #{a % b}."
I just tried it with a period and without. The code works will the period, and doesn't without it.
ANOOSHA CHIKKULAPALLI
3,152 PointsNow i got it. Thank you very much. but this is crazy that fate of the challenge was a period.
Cena Mayo
55,236 PointsYeah, I've been tripped up many times by some little thing like that! Usually when they want something specific printed to the screen in a challenge I just copy paste it into my code, to make sure I'm not missing something. :)
ANOOSHA CHIKKULAPALLI
3,152 PointsUse string interpolation to return the remainder inside the sentence “The remainder of a divided by b is c.” where a is your “a” variable, b is your “b” variable, and c is the value of a % b. This is the error am getting. Bummer! Something doesn't look quite right. Double check your spelling and punctuation.
def mod(a, b) #write your code here return "The remainder of #{a} divided by #{b} is #{a%b}" end
ANOOSHA CHIKKULAPALLI
3,152 PointsANOOSHA CHIKKULAPALLI
3,152 PointsStill it is not showing me the correct answer.
Cena Mayo
55,236 PointsCena Mayo
55,236 PointsWhat's the error you're getting? What's your code look like?