Python “魅力运动”;字符转义“:任务已完成,但语法错误

Python “魅力运动”;字符转义“:任务已完成,但语法错误,python,pycharm,Python,Pycharm,我正在遵循PyCharm Edu版本中的内置教程,我被字符串-字符转义所困扰。 在练习中,我被要求打印以下内容: 这种冰淇淋的名字叫“Sweeet'n'Tasty” 通过使用字符转义,下面是我的代码: print("The name of this ice-cream is \"Sweeet\'n\'Tasty\"") 它仍然会给我“对不起,打印错了字符串”。老实说,我不认为我打印了一个错误的字符串。有什么帮助吗?您必须退出“因为您在打印中使用它,但您的”不需要保护 打印“\'n”和“'n”将

我正在遵循PyCharm Edu版本中的内置教程,我被字符串-字符转义所困扰。 在练习中,我被要求打印以下内容: 这种冰淇淋的名字叫“Sweeet'n'Tasty” 通过使用字符转义,下面是我的代码:

print("The name of this ice-cream is \"Sweeet\'n\'Tasty\"")
它仍然会给我“对不起,打印错了字符串”。老实说,我不认为我打印了一个错误的字符串。有什么帮助吗?

您必须退出“因为您在打印中使用它,但您的”不需要保护

打印“\'n”和“'n”将输出相同的行,但转义即使不可见,也会生成一些由运动控制器读取的内容

请尝试在“”之前删除“”

print("The name of this ice-cream is \"Sweeet'n'Tasty\"")
包含“或”的字符串的另一种解决方案是使用三元组,如下所示:

print("""The name of this ice-cream is "Sweeet'n'Tasty\"""")
在这种情况下,句子被“力再次保护,但中间”不需要保护的事实。 您还可以颠倒使用“和”来保护“或”

也可以使用3’:

print('''The name of this ice-cream is "Sweeet'n'Tasty"''')
如果仍然不起作用,您可以提供断言测试吗

编辑:

这似乎是您面临的问题:

来自模糊的测试需求/IDE行为

dont_worry = "Don't worry about apostrophes"
print(dont_worry)
print("The name of this ice-cream is \"Sweeet\"")
print("The name of this ice-cream is \"Sweeet'n'Tasty\"")


print(“此冰淇淋的名称为“Sweeet'n'Tasty”)

最后一个print语句使用单引号,因此测试希望您仅转义围绕“n”的单引号,因此这一行对我有效:

print('The name of this ice-cream is "Sweeet\'n\'Tasty"')

只有使用双引号才能解决此问题(根据测试文件) 无论如何,如果您尝试此操作,它将帮助您成功完成:


print(“\'这个冰淇淋的名字是\'Sweeet'n'Tasty\””)

这些课程中的说明一点也不清楚,jetbrains的人应该对此进行调查。 即使他们在说明中没有提到,他们也希望您编辑第三行:

print("The name of this ice-cream is \"Sweeet\"")
为此:

print("The name of this ice-cream is \"Sweeet'n'Tasty\"")
无需在第四行中转义双引号:

print('The name of this ice-cream is "Sweeet\'n\'Tasty"')
所以完整的代码应该是这样的

dont_worry = "Don't worry about apostrophes"
print(dont_worry)
print("The name of this ice-cream is \"Sweeet'n'Tasty\"")
print('The name of this ice-cream is "Sweeet\'n\'Tasty"')
而且,这对我也很有用:
print(“这种冰淇淋的名字是“甜的”不“好吃的”)

我对编码(任何类型)都是新手,所以这只是一个猜测-解决方案要求您只打印一个字符串,所以可能
\“Sweet\n'Tasty\”
被解读为多个字符串?就像我说的,真正的初学者。(还有,Sweet可能拼写错误吗?

我也有同样的问题。 字符串是正确的,但它仍然说我得到了错误的字符串。然后我发现我的答案在答案占位符之外,只需重置任务,问题就解决了

print('The name of this ice-cream is "Sweet\'n\'Tasty"')

我使用了你的第三个解决方案,它应该是正确的,但仍然给我错误的文本。我如何提供测试?一个屏幕截图就足够了?然后只需要“保护”(第三个示例)请尝试添加另一条注释,而不是只编辑第一条注释。您可以给出准确的预期结果。根据每个练习文件夹中包含的tests.py文件,代码应为:print(“此冰淇淋的名称为\\\\“Sweeet'n'Tasty\”),然后预期输出应为
此冰淇淋的名称为\”Sweeet'n'Tasty\“
print('The name of this ice-cream is "Sweet\'n\'Tasty"')