Python Forum
Trouble making an argument optional
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble making an argument optional
#1
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:
Error:
E. ====================================================================== ERROR: test_first_last_middle_name (__main__.NamesTestCase) Do names like 'Wolfgang Amadeus Mozart' work? ---------------------------------------------------------------------- Traceback (most recent call last): File "I removed this file path", line 18, in test_first_last_middle_name 'wolfgang', 'mozart', 'amadeus') TypeError: get_formatted_name() takes 2 positional arguments but 3 were given ---------------------------------------------------------------------- Ran 2 tests in 0.000s
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!
Reply
#2
I think you accidentally saved a second version of your file or something. Works fine for me.
Output:
.. ---------------------------------------------------------------------- Ran 2 tests in 0.011s OK
Reply
#3
(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.
Output:
.. ---------------------------------------------------------------------- Ran 2 tests in 0.011s OK

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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,734 Mar-03-2020, 08:34 AM
Last Post: buran
  [split] Trouble making my first python .exe using pyinstaller. esha09 5 7,107 Apr-21-2018, 09:37 PM
Last Post: snippsat
  Trouble making my first python .exe using pyinstaller. PickyBiker 8 15,371 Dec-09-2016, 01:38 PM
Last Post: PickyBiker
  is 'self' optional ? meems 7 9,244 Dec-06-2016, 10:26 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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