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 trialmichaelangelo owildeberry
18,173 PointsCreate a function named to_json that takes a single argument. Convert the argument to string with the json library and r
is there alternative way for string that I am missing? please help =)
import json
apple = 10
def to_json():
return str(apple)
return apple
7 Answers
Raymond Wach
7,961 PointsAs you've defined it, to_json
currently takes no arguments. You need to declare a variable in the parameter list of your function; something like:
def foo(bar):
return bar
The next step is to use the appropriate method from the json
library to convert the argument to a JSON string. You will probably want to save that somehow or return the converted value.
Sean McKeown
23,267 PointsWell first you need to pass an argument to the to_json function. Then figure out what method that the json library provides that does the conversion for you.
michaelangelo owildeberry
18,173 PointsCreate a function named to_json that takes a single argument. Convert the argument to string with the json library and return it.
michaelangelo owildeberry
18,173 PointsBummer! to_json() takes 0 positional arguments but 1 was given
import json def to_json(): return json.stringify(old, new) return to_json
this worked out better, but did not pass. is there a different method to use?
michaelangelo owildeberry
18,173 Pointsyipee! =D the solution was simpler than first thought.
Craig Dennis
Treehouse TeacherHey I'm wondering....Why the two return statements?
michaelangelo owildeberry
18,173 PointsOne for conversion and one for relay =)
Craig Dennis
Treehouse TeacherYou can only really have one return statement. The first return will exit the function the following line will never be reached.