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 trialChristian Barry
Courses Plus Student 3,403 PointsHelp refactoring because spaghetti code (splitsville code challenge)
I completed the challenge but was wondering if it could be refactored for readability/better code.
Create a function called splitsville. It will take 1 parameter, an address. For example: '100 E > Main St, Anywhereville, Oregon, 22222'.
Split the string into the street, city, state, and zip.
Return a dictionary with the keys street, city, state, zip_code and the values from the split > string. Example: {'street': '100 E Main St', etc.}
def splitsville(address):
elements_dict = {"street": "", "city": "", "state": "", "zip_code": ""}
split = address.split(", ")
count = 0
for key in elements_dict.keys():
elements_dict.update({key: split[count]})
count += 1
return elements_dict
1 Answer
Steven Parker
231,264 PointsI'm not sure why this would be called "spaghetti code", it seems pretty concise.
My only suggestion for improvement is that the element names don't have to be a dictionary themselves. The could just be a plain list, which would eliminate needing to also store empty values.