Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print out text using one String in Python [closed]

I am learning Python by using Pycharm from Jetbrains . I am unable to pass this Question :

Print out this text using one String "The name of this ice-cream is "Sweet'n'Tasty"

My solution is:

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

It shows the right output but the program does not accept is as a solution.

Why the seemingly right answer is not accepted, and how to satisfy given requirement?

like image 702
Alexander Avatar asked Jan 09 '23 06:01

Alexander


1 Answers

I found this page because I ran into the exact same issue, earlier today.

In short, it's the lesson's logic being picky on syntax, in a very unclear way. You're typing the right thing for what it's asking, and you're typing something that parses with no error-- but the lesson rejects it anyway.

The long, drawn out answer.

The task is:

A backslash is used to escape a quote so it can be used in strings such as 'It\'s me' and "She said \"Hello\"". The special symbol '\n' is used to add a line break to a string.
Print out this text using one string: The name of this ice-cream is "Sweeet'n'Tasty"

And they provide to you:

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

where the text in the print('text') on the last line is meant to be replaced with your answer.

The lesson seems to want to teach you that you can escape both a single quote and a double quote with a backslash. Note the double quotes on the second print instance, and the single quote on the third print instance [where they want you to type your answer]

if you replace the text in print('text') with: The name of this ice-cream is \"Sweeet'n'Tasty\"

the task fails with:

File "/PycharmProjects/PythonIntroduction/lesson3/task8/character_escaping.py", line 4 print('The name of this ice-cream is \"Sweeet'n'Tasty\"') ^ SyntaxError: invalid syntax

so if you add slashes to the single quotes [apostrophes in this context] print('The name of this ice-cream is \"Sweeet\'n\'Tasty\"')

even though the text does parse correctly in the console below:

Don't worry about apostrophes
The name of this ice-cream is "Sweeet"
The name of this ice-cream is "Sweeet'n'Tasty"

The task fails and you get an infuriating error that reads:

Use backslash () to escape quotes

Even though that's what you've done.

If you try to backspace the single quotes to replace with double quotes for print('text') you get:

It's not allowed to delete answer placeholders

Now - if you move cursor [or caret] next to a quote, you get a lightbulb hint that gives you an option to:

Convert single quoted string to double-quoted string

which is what they want you to do ... but through some logic error, it takes your typed answer of

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

and munges it up to:

print("The name of this ice-cream is \\")

and the boundaries for the answer box text get all fouled up and wrap up to the previous line-- it looks like a bug in the lesson software itself.

SO

what you must do from the start is use your cursor to get the lighbulb hint FIRST and "Convert single quoted string to double-quoted string"

so that print('text') becomes print("text")

^^^ note the change from single to double quotes ^^^

and THEN type your correct answer of print("The name of this ice-cream is \"Sweeet'n'Tasty\"")

This took me MUCH longer than I would have liked it to, to figure it out. As a beginner in programming and brand-new to Python, this was a huge roadblock. If I were using this in an instructor-led course they might have said "Oh, this is a bug in the software, you can see we're getting the right answer, let's skip it and move on." but for self-study, I was convinced I wasn't right, and I was overlooking something basic. It was discouraging as slamming into a brick wall. Repeatedly.

I guess the teachable moment here is: Sometimes the textbook is wrong and you have to understand & prove it to yourself why.

like image 56
Roger McCubbins Avatar answered Jan 19 '23 13:01

Roger McCubbins