SyntaxError: invalid syntax error when copying and executing introductory code

Asked 2 years ago, Updated 2 years ago, 314 views

I'm a python beginner. I'm learning while copying the code from Python's introductory book, but I got an error and I can't proceed.I would appreciate it if you could tell me how to resolve the error.

The image below is the image of an error.
I copied the code twice, but I got an error.
Is it related to the version?
By the way, the version used in the introductory book is 3.7.Also, the introduction is called python introduction.

src=

python

2022-09-30 21:57

1 Answers

In the program, if is used to branch out the processing is incorrect.This is wrong for Python 2 or Python 3.

It looks like this now.

 print('The value is '+('positive'+ifn>0 else'0' ifn==0 else'negative')+'.')

Correctly, for example:The plus sign is missing.

 print('The value is '+' ('positive' if n>0 else' 0' if n == 0 else' negative') +'.')

I don't know if it was originally wrong or somewhere in the copy, but if you want to put an if in one line anyway, this is what it looks like.

SyntaxError, or SyntaxError, is an error that occurs when a program is written incorrectly.When this error occurs, read carefully around the error area indicated.


2022-09-30 21:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.