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