【问题标题】:How to check one string(including one empty space) with another string to get a Boolean output如何用另一个字符串检查一个字符串(包括一个空格)以获得布尔输出
【发布时间】:2021-03-26 03:52:27
【问题描述】:

如何检查一个字符串(包括一个空格)和另一个字符串以获得布尔输出

def check_win(puzzle: str, solution: str) -> bool:

    """
    Returns True if the game is won, given the puzzle and the solution, and
    False otherwise. Example of calling check_win:
        >>> check_win("abcdefgh ", "abcdefghi")
        True
        >>> check_win("dabecghf ", "abcdefghi")
        False

    Parameters:
        puzzle (str): a given string which needed to be matched with solution.
        solution (str): a solution string against the given puzzle

    Returns:
        (bool): returns true if two string matched, false otherwise. 
    """
    return puzzle == solution

对于第一个示例,它应该是 TRUE,但我得到的代码是 False,因为它没有将空格算作异常。

如何将第一个示例设为 TRUE,这意味着 ("abcdefgh ", "abcdefghi") 将空格视为第一个字符串中的异常?

谢谢

【问题讨论】:

  • 到目前为止,您尝试了哪些方法来解决您的问题?
  • 你能澄清一下究竟应该发生什么吗?空格只是最后的例外,还是字符串中的任何地方? puzzlesolution 是否总是相同的长度?一旦你澄清了,其他人会更容易提供帮助。你甚至可能会发现你可以自己解决它!
  • @sabik 空格是字符串中任何地方的异常。
  • @AllanWind,我已经做了````return (puzzle+" ") == 解决方案
  • 空格是否意味着任何字符都可以代替它?像第一种情况下的''=='i'?

标签: python string boolean


【解决方案1】:

我的猜测是你需要做的:

return solution in puzzle

这不是“字符串匹配”。

【讨论】:

    【解决方案2】:

    我整理好了。我不得不选择退出拼图和解决方案的最后一个索引,然后我运行了检查代码。

    谢谢大家

    【讨论】:

    • 它可能会通过你的测试,但我怀疑这是正确的。
    猜你喜欢
    • 2021-05-06
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 2013-05-12
    • 1970-01-01
    相关资源
    最近更新 更多