Trouble making an argument optional - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Trouble making an argument optional (/thread-12562.html) |
Trouble making an argument optional - linuxnoob - Aug-31-2018 So I'm using the book 'Python Crash Course' and working on the chapter dealing with testing your code. My function 'get_formatted_name' is supposed to accept 3 arguments. However 'middle' is supposed to be optional. I thought I did that correctly but when I test the code with unittest I'm getting an error. Here is get_formatted_name: def get_formatted_name(first, last, middle=''): """Generate a neatly formatted first and last name""" if middle: full_name = first + ' ' + middle + ' ' + last else: full_name = first + ' ' + last return full_name.title()Here is where I'm testing it: import unittest from name_function import get_formatted_name class NamesTestCase(unittest.TestCase): """Tests for 'name_function.py'.""" def test_first_last_name(self): """Do names like 'Janis Joplin' work?""" formatted_name = get_formatted_name('janis', 'joplin') self.assertEqual(formatted_name, 'Janis Joplin') def test_first_last_middle_name(self): """Do names like 'Wolfgang Amadeus Mozart' work?""" formatted_name = get_formatted_name( 'wolfgang', 'mozart', 'amadeus') self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart') unittest.main()And here is the error I'm getting: So I see where it says "takes 2 positional arguments but 3 were given". This leads me to believe I didn't successfully make 'middle' optional. But I did it just like the book shows for optional arguments and I'm still getting the error. Any ideas? Thanks in advance!
RE: Trouble making an argument optional - Mekire - Aug-31-2018 I think you accidentally saved a second version of your file or something. Works fine for me.
RE: Trouble making an argument optional - linuxnoob - Aug-31-2018 (Aug-31-2018, 01:29 AM)Mekire Wrote: I think you accidentally saved a second version of your file or something. Works fine for me. DING DING DING!!! Nailed it! I misspelled the file earlier, fixed it and forget that the old misspelled one was still hanging around. I've been beating my head against the wall on this for awhile. Thanks! |