简体   繁体   中英

Python print with string invalid syntax

I have a rock, paper, scissors code I've been working on lately (yes, I am a total noob at coding), and I get an Invalid Syntax error with this specific line:

print('The magical 8ball reads "Your fortune will come soon." It then turns over. It now reads "YOU'RE DEAD." You get suffocated by the paper.')

I don't know what's wrong and I haven't been able to find an answer. Any help would be gladly appreciated.

You have a single quote in the sentence causing the quote doesn't close properly.

Try this:

print('The magical 8ball reads "Your fortune will come soon." It then turns over. It now reads "YOU\'RE DEAD." You get suffocated by the paper.')

Using 3 single or double quotes to enclose the print statement.

print('''The magical 8ball reads "Your fortune will come soon." It then turns over. It now reads "YOU'RE DEAD." You get suffocated by the paper.''')
or
print("""The magical 8ball reads "Your fortune will come soon." It then turns over. It now reads "YOU'RE DEAD." You get suffocated by the paper.""")

I think You should try escape characters.

Let's try this:- print("The magical 8ball reads \\"Your fortune will come soon.\\" It then turns over. It now reads. \\"YOU'RE DEAD.\\" You get suffocated by the paper.")

If you try to debug on ur own you will see.. that a string is written in quotes. ' xyz ' or "xyz". You can not mix quotes likr. 'xy'z'. This is invalid as your string end at y only. To add apostrophe s you need to use backslash syntax and double quotes.

Something like - print('xy/'z')

/' is what you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM