Lỗi cú pháp python cú pháp không hợp lệ

>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2 trong Python có nghĩa là. chỉ chạy phần còn lại của mã này một lần, nếu điều kiện đánh giá là
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
4. Đừng chạy phần còn lại của mã nếu nó không

Cấu tạo của một câu lệnh

>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2. Bắt đầu với từ khóa
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2, theo sau là một giá trị boolean, một biểu thức có giá trị là
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
4 hoặc một giá trị có “Truthiness”. Thêm dấu hai chấm
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
8, một dòng mới và viết mã sẽ chạy nếu câu lệnh là
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
4 dưới mức thụt đầu dòng

Hãy nhớ rằng, giống như với các hàm, chúng ta biết rằng mã được liên kết với câu lệnh

>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2 bởi mức độ thụt đầu dòng của nó. Tất cả các dòng được thụt vào bên dưới câu lệnh
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2 sẽ chạy nếu nó đánh giá là
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
4

>>> if 3 < 5:
..     print("Hello, World!")
...
Hello, World!

Hãy nhớ rằng, các câu lệnh

>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2 của bạn chỉ chạy nếu biểu thức trong đó có giá trị là
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
4 và giống như với các hàm, bạn sẽ cần nhập thêm một khoảng trống trong REPL để chạy nó

Sử dụng
>>> message = "Hi there."

>>> a = 0
>>> if a:   # 0 is False-y
..     print(message)
...

>>> b = -1
>>> if b:  # -1 is Truth-y
..     print(message)
...
Hi there.

>>> c = []
>>> if c:  # Empty list is False-y
..     print(message)
...

>>> d = [1, 2, 3]
>>> if d:  # List with items is Truth-y
..     print(message)
...
Hi there.
5 với câu lệnh
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2

Nếu bạn chỉ muốn mã của mình chạy nếu biểu thức là

>>> message = "Hi there."

>>> a = 0
>>> if a:   # 0 is False-y
..     print(message)
...

>>> b = -1
>>> if b:  # -1 is Truth-y
..     print(message)
...
Hi there.

>>> c = []
>>> if c:  # Empty list is False-y
..     print(message)
...

>>> d = [1, 2, 3]
>>> if d:  # List with items is Truth-y
..     print(message)
...
Hi there.
7, hãy sử dụng từ khóa
>>> message = "Hi there."

>>> a = 0
>>> if a:   # 0 is False-y
..     print(message)
...

>>> b = -1
>>> if b:  # -1 is Truth-y
..     print(message)
...
Hi there.

>>> c = []
>>> if c:  # Empty list is False-y
..     print(message)
...

>>> d = [1, 2, 3]
>>> if d:  # List with items is Truth-y
..     print(message)
...
Hi there.
5

>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!

>>> b = False >>> if not b: .. print("Negation in action!") ... Negation in action! 2 Tuyên bố và Sự thật

>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2 câu lệnh cũng có tác dụng với những mục có tính “trung thực” đối với chúng

Ví dụ

  • Số 0 là Sai-y, bất kỳ số nào khác (bao gồm cả số âm) là Đúng-y
  • Một khoảng trống
    >>> def modify_name(name):
    ..    if len(name) < 5:
    ..         return name.upper()
    ..    else:
    ..         return name.lower()
    ...
    >>> name = "Nina"
    >>> modify_name(name)
    'NINA'
    
    1,
    >>> def modify_name(name):
    ..    if len(name) < 5:
    ..         return name.upper()
    ..    else:
    ..         return name.lower()
    ...
    >>> name = "Nina"
    >>> modify_name(name)
    'NINA'
    
    2,
    >>> def modify_name(name):
    ..    if len(name) < 5:
    ..         return name.upper()
    ..    else:
    ..         return name.lower()
    ...
    >>> name = "Nina"
    >>> modify_name(name)
    'NINA'
    
    3 hoặc
    >>> def modify_name(name):
    ..    if len(name) < 5:
    ..         return name.upper()
    ..    else:
    ..         return name.lower()
    ...
    >>> name = "Nina"
    >>> modify_name(name)
    'NINA'
    
    4 là Sai-y
  • Bất kỳ cấu trúc nào có vật phẩm trong đó đều là Truth-y
>>> message = "Hi there."

>>> a = 0
>>> if a:   # 0 is False-y
..     print(message)
...

>>> b = -1
>>> if b:  # -1 is Truth-y
..     print(message)
...
Hi there.

>>> c = []
>>> if c:  # Empty list is False-y
..     print(message)
...

>>> d = [1, 2, 3]
>>> if d:  # List with items is Truth-y
..     print(message)
...
Hi there.

>>> b = False >>> if not b: .. print("Negation in action!") ... Negation in action! 2 Câu lệnh và Hàm

Bạn có thể dễ dàng khai báo

>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2 câu lệnh trong hàm của mình, bạn chỉ cần lưu ý đến mức độ thụt đầu dòng. Lưu ý cách mã thuộc câu lệnh
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2 được thụt vào ở hai cấp độ

>>> def modify_name(name):
..    if len(name) < 5:
..         return name.upper()
..    else:
..         return name.lower()
...
>>> name = "Nina"
>>> modify_name(name)
'NINA'

Các câu lệnh
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2 lồng nhau

Sử dụng kỹ thuật tương tự, bạn cũng có thể lồng các câu lệnh

>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2 của mình

>>> def num_info(num):
..    if num > 0:
..        print("Greater than zero")
..        if num > 10:
..            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.

Làm thế nào để không sử dụng câu lệnh
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2

Hãy nhớ rằng, phép so sánh trong Python đánh giá bằng

>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
4 hoặc
>>> message = "Hi there."

>>> a = 0
>>> if a:   # 0 is False-y
..     print(message)
...

>>> b = -1
>>> if b:  # -1 is Truth-y
..     print(message)
...
Hi there.

>>> c = []
>>> if c:  # Empty list is False-y
..     print(message)
...

>>> d = [1, 2, 3]
>>> if d:  # List with items is Truth-y
..     print(message)
...
Hi there.
7. Với các câu điều kiện, chúng tôi kiểm tra ngầm giá trị đó. Trong Python, chúng tôi không muốn so sánh
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
4 hoặc
>>> message = "Hi there."

>>> a = 0
>>> if a:   # 0 is False-y
..     print(message)
...

>>> b = -1
>>> if b:  # -1 is Truth-y
..     print(message)
...
Hi there.

>>> c = []
>>> if c:  # Empty list is False-y
..     print(message)
...

>>> d = [1, 2, 3]
>>> if d:  # List with items is Truth-y
..     print(message)
...
Hi there.
7 với
>>> def num_info(num):
..    if num > 0:
..        print("Greater than zero")
..        if num > 10:
..            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.
5

Cảnh báo - hãy chú ý, vì đoạn mã dưới đây cho biết những gì bạn không nên làm

# Warning: Don't do this!
>>> if (3 < 5) == True: # Warning: Don't do this!
..     print("Hello")
...
Hello

# Warning: Don't do this!
>>> if (3 < 5) is True: # Warning: Don't do this!
..     print("Hello")
...
Hello
>>> if 3 < 5:
..     print("Hello")
...
Hello

Nếu chúng tôi muốn kiểm tra rõ ràng xem giá trị có được đặt rõ ràng thành

>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
4 hoặc
>>> message = "Hi there."

>>> a = 0
>>> if a:   # 0 is False-y
..     print(message)
...

>>> b = -1
>>> if b:  # -1 is Truth-y
..     print(message)
...
Hi there.

>>> c = []
>>> if c:  # Empty list is False-y
..     print(message)
...

>>> d = [1, 2, 3]
>>> if d:  # List with items is Truth-y
..     print(message)
...
Hi there.
7 hay không, chúng tôi có thể sử dụng từ khóa
>>> def num_info(num):
..    if num > 0:
..        print("Greater than zero")
..        if num > 10:
..            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.
8

>>> a = True        # a is set to True
>>> b = [1, 2, 3]   # b is a list with items, is "truthy"
>>>
>>> if a and b:     # this is True, a is True, b is "truthy"
..     print("Hello")
...
Hello
>>> if a is True:   # we can explicitly check if a is True
..     print("Hello")
...
Hello
>>> if b is True:   # b does not contain the actual value of True.
..     print("Hello")
...
>>>

Câu lệnh

>>> def num_info(num):
..    if num > 0:
..        print("Greater than zero")
..        if num > 10:
..            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.
9 là những gì bạn muốn chạy khi và chỉ khi câu lệnh
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2 của bạn không được kích hoạt

Câu lệnh

>>> def num_info(num):
..    if num > 0:
..        print("Greater than zero")
..        if num > 10:
..            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.
9 là một phần của câu lệnh
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2. Nếu câu lệnh
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2 của bạn chạy, câu lệnh
>>> def num_info(num):
..    if num > 0:
..        print("Greater than zero")
..        if num > 10:
..            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.
9 của bạn sẽ không bao giờ chạy

>>> a = True
>>> if a:
..     print("Hello")
.. else:
..     print("Goodbye")
...
Hello

Và ngược lại

>>> a = False
>>> if a:
..     print("Hello")
.. else:
..     print("Goodbye")
...
Goodbye

Trong REPL, nó phải được viết trên dòng sau dòng mã thụt vào cuối cùng của bạn. Trong mã Python trong một tệp, không thể có bất kỳ mã nào khác giữa

>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2 và
>>> def num_info(num):
..    if num > 0:
..        print("Greater than zero")
..        if num > 10:
..            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.
9

Bạn sẽ thấy

# Warning: Don't do this!
>>> if (3 < 5) == True: # Warning: Don't do this!
..     print("Hello")
...
Hello

# Warning: Don't do this!
>>> if (3 < 5) is True: # Warning: Don't do this!
..     print("Hello")
...
Hello
7 nếu bạn cố gắng tự viết một câu lệnh
>>> def num_info(num):
..    if num > 0:
..        print("Greater than zero")
..        if num > 10:
..            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.
9 hoặc đặt mã bổ sung giữa
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2 và
>>> def num_info(num):
..    if num > 0:
..        print("Greater than zero")
..        if num > 10:
..            print("Also greater than 10.")
...
>>> num_info(1)
Greater than zero
>>> num_info(15)
Greater than zero
Also greater than 10.
9 trong tệp Python

>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
0

>>> if 3 < 5: .. print("Hello") ... Hello 1 Có nghĩa là Khác, Nếu

>>> if 3 < 5:
..     print("Hello")
...
Hello
1 có nghĩa là khác nếu. Điều đó có nghĩa là, nếu câu lệnh
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
2 này không được coi là
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
4, hãy thử câu lệnh này để thay thế

Bạn có thể có bao nhiêu câu lệnh

>>> if 3 < 5:
..     print("Hello")
...
Hello
1 trong mã của mình tùy thích. Chúng được đánh giá theo thứ tự mà chúng được khai báo cho đến khi Python tìm thấy một thứ là
>>> b = False
>>> if not b:
..     print("Negation in action!")
...
Negation in action!
4. Điều đó chạy mã được xác định trong
>>> if 3 < 5:
..     print("Hello")
...
Hello
1 đó và bỏ qua phần còn lại

Tại sao Python của tôi nói cú pháp không hợp lệ?

Một số nguyên nhân phổ biến nhất gây ra lỗi cú pháp trong Python là. Từ khóa dành riêng sai chính tả . Thiếu dấu ngoặc kép. Thiếu khoảng trống bắt buộc.

Tại sao mã của tôi nói cú pháp không hợp lệ?

Nguyên nhân gây ra lỗi cú pháp. cú pháp không hợp lệ . ) ở cuối dòng hoặc trộn lẫn các ký hiệu khác Missing a colon ( : ) at the end of a line or mixing up other symbols . Thiếu dấu ngoặc đơn mở hoặc đóng ( (. ) ), dấu ngoặc vuông ( [. ] ), niềng răng ( {. } ) hoặc dấu ngoặc kép ( ". " ) Sai chính tả hoặc thiếu từ khóa hoặc nhập sai cú pháp trong một khối hoặc biểu thức.