【发布时间】:2019-02-21 19:56:49
【问题描述】:
我正在学习使用 Exercism 在 Python 3.x 上编写代码。我必须编写一个条件(在辅助文件上)以根据从主函数发送的单词返回一个字符串:
用户功能:
def two_fer(name):
if name:
string = "One for " + name + ", one for me."
else:
string = "One for you, one for me."
return string
主要功能:
class TwoFerTest(unittest.TestCase):
def test_no_name_given(self):
self.assertEqual(two_fer(), 'One for you, one for me.')
def test_a_name_given(self):
self.assertEqual(two_fer("Alice"), "One for Alice, one for me.")
def test_another_name_given(self):
self.assertEqual(two_fer("Bob"), "One for Bob, one for me.")
问题是,对于main函数中的第一个条件,它调用two_fer()没有条件,导致失败。
main函数应该是不能修改的,有什么办法只能通过user函数解决问题吗?
提前致谢。
【问题讨论】:
-
对参数使用默认值?
-
是的,如上所述,您可以通过在参数函数中使用添加默认值(参数=某物),也可以使用lambda函数调用带参数的普通函数
-
我觉得你需要
def two_fer(name=""):
标签: python python-3.x error-handling conditional