If you use the print function, it doesn't run as you want, and you get an error called SyntaxError: invalid syntax.

Asked 2 years ago, Updated 2 years ago, 17 views

If you use the print function, it doesn't run as you want, and you get an error called SyntaxError: invalid syntax.

This is it

python

2022-09-30 11:12

1 Answers

It seems that it is difficult to understand because the occurrence of the error has changed since it is in the Google collaboration environment.(Actually, the reason seems to be the number of Python versions used in the environment.→See postscript)
This is due to the absence of the closing parenthesis ) of the previous line, not the line pointed out as SyntaxError in Google Collaboration as stated in the comment.

This is the code if you shorten it to the smallest part that the problem reproduces.

mein_card="Dinosaur"
print("You are a B-user.Your character is "
print(mein_card)

If you reproduce the appearance in the Google Collaboration environment in text, you will see an error like this:

mein_card="Dinosaur"
print("You are a B-user.Your character is "
print(mein_card)
File"<ipython-input-1-9a888d9a61f2>", line 3
    print(mein_card)
        ^
SyntaxError: invalid syntax

SEARCH STACK OVERFLOW

The command prompt on the local PC looks like this and points out the problem directly.

 Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug 1222, 21:53:49) [MSC v.1932 64bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>mein_card="Dinosaur"
>>print("You are a B-user.Your character is "
... print(mein_card)
  File "<stdin>", line 1
    print("You are a B-user.Your character is "
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax.Perhaps you forgot a comma?
>>

Recognize that the Google Collaboration environment may display errors when they are out of alignment with those issues, and be careful not to see any problems around them.

add

@mjy pointed out that the reason is not because of the Google collaboration environment, but because of the difference in the number of Python versions.

Currently, Python is 3.7.13 for Google collaboration.

When I tried running Python in the 3.9.13 environment at the local PC command prompt, I got the same error as the question.

So I looked into What's New In Python 3.10, and it seems that the following items are affected.
Better error messages

This means that everything under Python 3.10 series, regardless of the Google Collaboration environment, is affected and needs to be careful.


2022-09-30 11:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.