Python Forum
New to python/coding Need help on Understanding why this code isn't working. Thanks!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to python/coding Need help on Understanding why this code isn't working. Thanks!
#6
You could print the return value if you want to see the results.

As for "the wrong way", using a mutable type for a default argument can cause problems, but it does not in your code. The default argument is there to protect against calling the function without any arguments. I think it makes more sense to let the function crash if no list is provided, but the author decided they would return an empty string when this happens.

A larger problem with the code is it limits you to sorting lists. You cannot sort a tuple or a set. I would rewrite to make it work with any itrator.
def alphabetize(items=[]):
    """Return sorted list as CSV string."""
    final_list = ''
    for name in sorted(items):
        final_list += name + ', '
    final_list = final_list[:-2]
    return final_list


names = {'McMullen', 'Keaser', 'Maier', 'Wilson', 'Yudt', 'Gallagher', 'Jacobs'}
print(alphabetize(names))
Using "sorted()" instead of "sort()" we don't have to copy the input list. sorted() creates the new, sorted list for us. Since we don't have to copy() the input, we don't have to pass in a list. In my example I passed in a set. I could also pass a tuple, or a generator, or any iterable (thing that can be used in a for loop).

Another problem is the function can be replaced with a single line of code.
names = {'McMullen', 'Keaser', 'Maier', 'Wilson', 'Yudt', 'Gallagher', 'Jacobs'}
print(",".join(sorted(names)))
But these are only problems if this was "production code", not if it is a coding example for learning Python. It is difficult to write code that is both well designed and demonstrates the intent of the lesson. Maybe the next lesson will point out the shortcomings of limiting the function input to a list, and the advantages of using the builtin function and standard libraries instead of writing your own.
Reply


Messages In This Thread
RE: New to python/coding Need help on Understanding why this code isn't working. Thanks! - by deanhystad - May-08-2023, 12:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Coding help required in Python using VS Code KakashiSenpai 1 138 Yesterday, 11:23 AM
Last Post: gerpark
  updating pip, python - understanding terminal and pycharm. endlessnameless 0 184 Jun-24-2024, 05:48 AM
Last Post: endlessnameless
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 1,157 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 1,125 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  Code understanding: Need help in understanding dictionary code jt123 0 563 Jul-09-2023, 01:13 PM
Last Post: jt123
  Understanding venv; How do I ensure my python script uses the environment every time? Calab 1 2,584 May-10-2023, 02:13 PM
Last Post: Calab
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,185 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  Understanding Python classes PythonNewbee 3 1,365 Nov-10-2022, 11:07 PM
Last Post: deanhystad
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,236 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 2,129 Mar-21-2022, 10:12 AM
Last Post: End3r

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020