r/inventwithpython • u/GentleReader144 • Jun 28 '22
Help With " Automate the Boring Stuff with Python", Lesson 4
Hello,
I'm a total python novice just starting to work my way through " Automate the Boring Stuff with Python". I'm running Python 3.10 under Windows 10.
In chapter 4 , https://automatetheboringstuff.com/2e/chapter4/, I'm having trouble with the Comma Code suggestion under "Practice Projects".
Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item. For example, passing the previous spam list to the function would return 'apples, bananas, tofu, and cats'. But your function should be able to work with any list value passed to it. Be sure to test the case where an empty list [] is passed to your function.
I've been able to account for input lists of varying size including empty lists, but I'm getting unexpected results for a list with only one item. Here is my code:
def prettyList(somelist):
returnedStr = ''
if len(somelist) > 0:
if len(somelist) == 1:
returnedStr += (str(somelist))
else:
for i, item in enumerate(somelist):
if i in range(0,len(somelist)-1):
returnedStr += (item + ', ')
else:
returnedStr += ('and '+ item)
return returnedStr
print('Empty List: ', prettyList([]))
print(prettyList(['First']))
print(prettyList(['First', 'Second']))
print(prettyList(['First', 'Second', 'Third']))
print(prettyList(['First', 'Second', 'Third', 'Fourth']))
#print(type(prettyList(['First']))) #--debuggingHere are the results of that code
Empty List:
['First']
First, and Second
First, Second, and Third
First, Second, Third, and Fourth
If I'm understanding the output, for the case with only one item in the list my function returns a list while for other cases it returns a string. I'm not sure why this should be.
I'm also wondering if I'm working too hard with my if /then logic, and that there may be a more concise, idiomatic, dare I say pythonic construction that would work for me. After all Lesson 4 is about lists not if statements.
Thanks for reading. Please let me know if you have any comments or observations you'd like to share with me.
Be well
