Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何编写程序来检查作为参数提供的单词是否为等值线_Python_Methods_Boolean_Tuples - Fatal编程技术网

Python 如何编写程序来检查作为参数提供的单词是否为等值线

Python 如何编写程序来检查作为参数提供的单词是否为等值线,python,methods,boolean,tuples,Python,Methods,Boolean,Tuples,我需要编写这段python代码来检查一个单词是否是等值线,并被要求使用一个方法并调用一个元组,然后以布尔值结束。这是我得到的测试代码,我已经和这个代码斗争了一天多,没有解决方案 from unittest import TestCase class IsogramTestCases(TestCase): def test_checks_for_isograms(self): word = 'abolishment' self.assertEqual( is_is

我需要编写这段python代码来检查一个单词是否是等值线,并被要求使用一个方法并调用一个元组,然后以布尔值结束。这是我得到的测试代码,我已经和这个代码斗争了一天多,没有解决方案

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'
      )

您可以使用
len(set(s))==len(s)
测试等值线图,也就是说,如果唯一字符集的长度等于字符串的长度,则字符串是等值线图

在我看来,函数应该是这样的:

def is_isogram(s):
    if not isinstance(s, str):
       raise TypeError('String inputs allowed only')
    return len(set(s)) == len(s)

您测试过但不起作用的代码在哪里。很难知道到底出了什么问题?欢迎来到StackOverflow。请阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。您能否解释所显示代码的相关性,并给出所需输入和输出的示例?