任务:
编写一个程序,检查作为参数提供的单词是否是等值线.Isogram是一个不会出现多次字母的单词.
创建一个名为is_isogram的方法,它接受一个参数,一个单词来测试它是否是等值线图.此方法应返回单词的元组和布尔值,指示它是否是等值线图.
如果提供的参数是空字符串,则返回参数和False :(参数,False).如果提供的参数不是字符串,则引发TypeError,并显示消息'Argument should a a string'.
例:
is_isogram("abolishment")
Run Code Online (Sandbox Code Playgroud)
预期结果:
("abolishment", True)
Run Code Online (Sandbox Code Playgroud)
可见测试
from unittest import TestCase
class IsogramTestCases(TestCase):
def test_checks_for_isograms(self):
word = 'abolishment'
self.assertEqual(
is_isogram(word),
(word, True),
msg="Isogram word, '{}' not detected correctly".format(word)
)
def test_returns_false_for_nonisograms(self):
word = 'alphabet'
self.assertEqual(
is_isogram(word),
(word, False),
msg="Non isogram word, '{}' falsely detected".format(word)
)
def test_it_only_accepts_strings(self):
with self.assertRaises(TypeError) as context:
is_isogram(2)
self.assertEqual(
'Argument should be a string',
context.exception.message,
'String inputs allowed only'
)
Run Code Online (Sandbox Code Playgroud)
我的解决方案
def is_isogram(word):
if type(word) != str: …Run Code Online (Sandbox Code Playgroud) python ×1