pycharm练习"Character Escaping":任务已完成但语法错误



我正在学习PyCharm Edu Edition中的内置教程,我一直在学习字符串-字符转义。在练习中,我被要求打印以下内容:这种冰淇淋的名字叫"Sweeet'n'Lasty"通过使用字符转义,下面是我的代码:

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

它仍然会告诉我"对不起,打印错了字符串"。老实说,我不认为我打印错了字符串。有什么帮助吗?

最后一个print语句使用单引号,因此测试希望您只转义'n'周围的单引号,所以这行对我有效:

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

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

打印"\'n"one_answers"'n"将输出同一行,但转义符(即使不可见(将生成可由您的锻炼控制器读取的内容。

尝试删除之前的\

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

对于包含"或"的字符串,另一种解决方案是使用类似于这样的三元组:

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

在这种情况下,判决被"再次保护的力量"终止,但中间的"不需要保护"。

您还可以颠倒使用"one_answers"来保护

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

使用3'也是可能的:

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

如果这仍然不起作用,你能提供断言测试吗?

编辑:

这似乎是您面临的问题:http://iswwwup.com/t/d08b1b05234e/print-out-text-using-one-string-in-python.html

来自模糊的测试要求/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"")


打印("这个冰淇淋的名字是"Sweeet'n'Lasty"(

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

print("'The name of this ice-cream is "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("The name of this ice-cream is "Sweet'n'Tasty"")

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

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

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

最新更新