断言语句 returns False 即使返回的字符串似乎与断言的字符串相同
Assert statement returns False even though the returned string seems to be identical to the asserting one
下面的 class 允许创建和编辑文本对象有三种方法 - write()
向现有文本添加新文本,set_font
在正方形中添加新字体的名称括号放在文本的开头和结尾,show()
只是打印文本:
import re
class Text:
def __init__(self):
self.content = ''
def write(self, text):
match = re.search(r'\[[^\]]*\]$', self.content, re.IGNORECASE)
if match:
font_name = match.group(0)
self.content = font_name + self.content.replace(font_name, '') + text + font_name
else:
self.content += text
def set_font(self, font_name):
new_font = "[{}]".format(font_name)
match = re.match(r'\[[^\]]*\]', self.content, re.IGNORECASE)
if match:
old_font = match.group(0)
self.content = self.content.replace(old_font, new_font)
else:
self.content = new_font + self.content + new_font
def show(self):
print(self.content)
当我在下面的代码中创建和操作对象时,它似乎按预期工作,但它没有通过下面的断言测试,甚至认为它似乎输出与assert
语句。
你能帮我看看我错过了什么吗?
text = Text()
text.write("At the very beginning ")
text.set_font("Arial")
text.write("there was nothing.")
assert text.show() == "[Arial]At the very beginning there was nothing.[Arial]"
text.show 不是 return 值,它只是打印结果。
Python函数会returnNone,然后将None与需要的字符串进行比较,结果为False。
下面的 class 允许创建和编辑文本对象有三种方法 - write()
向现有文本添加新文本,set_font
在正方形中添加新字体的名称括号放在文本的开头和结尾,show()
只是打印文本:
import re
class Text:
def __init__(self):
self.content = ''
def write(self, text):
match = re.search(r'\[[^\]]*\]$', self.content, re.IGNORECASE)
if match:
font_name = match.group(0)
self.content = font_name + self.content.replace(font_name, '') + text + font_name
else:
self.content += text
def set_font(self, font_name):
new_font = "[{}]".format(font_name)
match = re.match(r'\[[^\]]*\]', self.content, re.IGNORECASE)
if match:
old_font = match.group(0)
self.content = self.content.replace(old_font, new_font)
else:
self.content = new_font + self.content + new_font
def show(self):
print(self.content)
当我在下面的代码中创建和操作对象时,它似乎按预期工作,但它没有通过下面的断言测试,甚至认为它似乎输出与assert
语句。
你能帮我看看我错过了什么吗?
text = Text()
text.write("At the very beginning ")
text.set_font("Arial")
text.write("there was nothing.")
assert text.show() == "[Arial]At the very beginning there was nothing.[Arial]"
text.show 不是 return 值,它只是打印结果。
Python函数会returnNone,然后将None与需要的字符串进行比较,结果为False。