Hướng dẫn why does python use elif and not else if? - tại sao python lại sử dụng elif mà không phải nếu khác?

Theo mặc định, các câu lệnh trong tập lệnh được thực thi tuần tự từ lần đầu tiên đến cuối cùng. Nếu logic xử lý yêu cầu như vậy, luồng tuần tự có thể được thay đổi theo hai cách:

Python sử dụng từ khóa

price = 50

if price < 100:
    print("price is less than 100")
7 để thực hiện kiểm soát quyết định. Cú pháp của Python để thực hiện một khối có điều kiện như dưới đây:

if [boolean expression]:
    statement1
    statement2
    ...
    statementN

Bất kỳ biểu thức boolean nào đánh giá thành

price = 50

if price < 100:
    print("price is less than 100")
8 hoặc
price = 50

if price < 100:
    print("price is less than 100")
9 xuất hiện sau từ khóa
price = 50

if price < 100:
    print("price is less than 100")
7. Sử dụng ký hiệu
price = 50
quantity = 5
if price*quantity < 500:
    print("price*quantity is less than 500")
    print("price = ", price)
    print("quantity = ", quantity)
1 và nhấn ENTER sau khi biểu thức để bắt đầu một khối với phần thụt lề tăng lên. Một hoặc nhiều câu lệnh được viết với cùng một mức thụt sẽ được thực thi
price = 50

if price < 100:
    print("price is less than 100")
7 Biểu thức Boolean đánh giá thành
price = 50

if price < 100:
    print("price is less than 100")
8.

Để kết thúc khối, giảm thụt lề. Các câu lệnh tiếp theo sau khi khối sẽ được thực thi khỏi điều kiện

price = 50

if price < 100:
    print("price is less than 100")
7. Ví dụ sau đây cho thấy điều kiện
price = 50

if price < 100:
    print("price is less than 100")
7.

price = 50

if price < 100:
    print("price is less than 100")

Trong ví dụ trên, biểu thức

price = 50
quantity = 5
if price*quantity < 500:
    print("price*quantity is less than 500")
    print("price = ", price)
    print("quantity = ", quantity)
6 đánh giá thành
price = 50

if price < 100:
    print("price is less than 100")
8, do đó nó sẽ thực thi khối. Khối
price = 50

if price < 100:
    print("price is less than 100")
7 bắt đầu từ dòng mới sau
price = 50
quantity = 5
if price*quantity < 500:
    print("price*quantity is less than 500")
    print("price = ", price)
    print("quantity = ", quantity)
1 và tất cả các câu lệnh trong điều kiện
price = 50

if price < 100:
    print("price is less than 100")
7 bắt đầu với một vết lõm tăng, không gian hoặc tab. Ở trên, khối
price = 50

if price < 100:
    print("price is less than 100")
7 chỉ chứa một câu lệnh. Ví dụ sau đây có nhiều câu trong điều kiện if.

price = 50
quantity = 5
if price*quantity < 500:
    print("price*quantity is less than 500")
    print("price = ", price)
    print("quantity = ", quantity)

price*quantity is less than 500
price = 50
quantity = 5

Ở trên, điều kiện IF chứa nhiều câu lệnh có cùng thụt. Nếu tất cả các câu lệnh không nằm trong cùng một thụt lề, thì không gian hoặc một tab thì nó sẽ tăng

price*quantity is less than 500
price = 50
quantity = 5
2.

price = 50
quantity = 5
if price*quantity < 500:
    print("price is less than 500")
    print("price = ", price)
     print("quantity = ", quantity)

  print("quantity = ", quantity)
 ^
IdentationError: unexpected indent

Các báo cáo có cùng mức thụt với điều kiện

price = 50

if price < 100:
    print("price is less than 100")
7 sẽ không xem xét trong khối if. Họ sẽ xem xét ra khỏi điều kiện
price = 50

if price < 100:
    print("price is less than 100")
7.

price = 50
quantity = 5
if price*quantity < 100:
    print("price is less than 500")
    print("price = ", price)
    print("quantity = ", quantity)
print("No if block executed.")

Ví dụ sau đây cho thấy nhiều điều kiện nếu điều kiện.

price = 100

if price > 100:
 print("price is greater than 100")

if price == 100:
  print("price is 100")

if price < 100:
    print("price is less than 100")

Lưu ý rằng mỗi khối

price = 50

if price < 100:
    print("price is less than 100")
7 chứa một câu lệnh trong một vết lõm khác nhau và điều đó hợp lệ vì chúng khác nhau.

Nên sử dụng 4 khoảng trống hoặc một tab làm cấp độ thụt mặc định để có thể dễ đọc hơn.

điều kiện khác

Cùng với câu lệnh

price = 50

if price < 100:
    print("price is less than 100")
7, điều kiện
price*quantity is less than 500
price = 50
quantity = 5
7 có thể được sử dụng tùy chọn để xác định một khối các câu lệnh thay thế sẽ được thực thi nếu biểu thức boolean trong điều kiện
price = 50

if price < 100:
    print("price is less than 100")
7 đánh giá là
price = 50

if price < 100:
    print("price is less than 100")
9.

if [boolean expression]:
    statement1
    statement2
    ...
    statementN
else:
    statement1
    statement2
    ...
    statementN

Như đã đề cập trước đó, khối thụt lề bắt đầu sau biểu tượng

price = 50
quantity = 5
if price*quantity < 500:
    print("price*quantity is less than 500")
    print("price = ", price)
    print("quantity = ", quantity)
1, sau biểu thức boolean. Nó sẽ được thực thi khi điều kiện là
price = 50

if price < 100:
    print("price is less than 100")
8. Chúng tôi có một khối khác nên được thực thi khi điều kiện
price = 50

if price < 100:
    print("price is less than 100")
7 là
price = 50

if price < 100:
    print("price is less than 100")
9. Đầu tiên, hãy hoàn thành khối
price = 50

if price < 100:
    print("price is less than 100")
7 bằng không gian ngược và viết
price*quantity is less than 500
price = 50
quantity = 5
7, đặt thêm ký hiệu
price = 50
quantity = 5
if price*quantity < 500:
    print("price*quantity is less than 500")
    print("price = ", price)
    print("quantity = ", quantity)
1 ở phía trước khối mới để bắt đầu nó và thêm các câu lệnh cần thiết vào khối.

price = 50

if price >= 100:
    print("price is greater than 100")
else:
    print("price is less than 100")

Trong ví dụ trên, điều kiện IF

price = 50
quantity = 5
if price*quantity < 500:
    print("price is less than 500")
    print("price = ", price)
     print("quantity = ", quantity)
7 là
price = 50

if price < 100:
    print("price is less than 100")
9, do đó khối
price*quantity is less than 500
price = 50
quantity = 5
7 sẽ được thực thi. Khối khác cũng có thể chứa nhiều câu lệnh có cùng thụt lề; Nếu không, nó sẽ tăng
  print("quantity = ", quantity)
 ^
IdentationError: unexpected indent
0.

Lưu ý rằng bạn không thể có nhiều khối

price*quantity is less than 500
price = 50
quantity = 5
7 và nó phải là khối cuối cùng.

điều kiện Elif

Sử dụng điều kiện

  print("quantity = ", quantity)
 ^
IdentationError: unexpected indent
2 được sử dụng để bao gồm nhiều biểu thức có điều kiện sau điều kiện
price = 50

if price < 100:
    print("price is less than 100")
7 hoặc giữa các điều kiện
price = 50

if price < 100:
    print("price is less than 100")
7 và
price*quantity is less than 500
price = 50
quantity = 5
7.

price = 50

if price < 100:
    print("price is less than 100")
0

Khối

  print("quantity = ", quantity)
 ^
IdentationError: unexpected indent
2 được thực thi nếu điều kiện được chỉ định đánh giá là
price = 50

if price < 100:
    print("price is less than 100")
8.

price = 50

if price < 100:
    print("price is less than 100")
1

Trong ví dụ trên, các điều kiện

  print("quantity = ", quantity)
 ^
IdentationError: unexpected indent
2 được áp dụng sau điều kiện
price = 50

if price < 100:
    print("price is less than 100")
7. Python sẽ đánh giá điều kiện
price = 50

if price < 100:
    print("price is less than 100")
7 và nếu nó đánh giá thành
price = 50

if price < 100:
    print("price is less than 100")
9 thì nó sẽ đánh giá các khối
  print("quantity = ", quantity)
 ^
IdentationError: unexpected indent
2 và thực hiện khối
  print("quantity = ", quantity)
 ^
IdentationError: unexpected indent
2 có biểu thức đánh giá là
price = 50

if price < 100:
    print("price is less than 100")
8. Nếu nhiều điều kiện
  print("quantity = ", quantity)
 ^
IdentationError: unexpected indent
2 trở thành
price = 50

if price < 100:
    print("price is less than 100")
8, thì khối
  print("quantity = ", quantity)
 ^
IdentationError: unexpected indent
2 đầu tiên sẽ được thực thi.

Ví dụ sau đây cho thấy nếu, Elif, và các điều kiện khác.

price = 50

if price < 100:
    print("price is less than 100")
2

Tất cả các điều kiện IF, ELIF và các điều kiện khác phải bắt đầu từ cùng một mức thụt, nếu không nó sẽ tăng

  print("quantity = ", quantity)
 ^
IdentationError: unexpected indent
0.

price = 50

if price < 100:
    print("price is less than 100")
3

price = 50

if price < 100:
    print("price is less than 100")
4

Lồng nhau nếu, elif, điều kiện khác

Python hỗ trợ lồng nhau nếu, Elif, và điều kiện khác. Điều kiện bên trong phải có vết lõm tăng so với điều kiện bên ngoài và tất cả các câu lệnh trong một khối phải có cùng thụt.

price = 50

if price < 100:
    print("price is less than 100")
5

price = 50

if price < 100:
    print("price is less than 100")
6

Tại sao tốt hơn là sử dụng Elif thay vì nếu?

ELIF là viết tắt cho người khác nếu.Nó cho phép chúng tôi kiểm tra nhiều biểu thức.Nếu điều kiện nếu là sai, nó sẽ kiểm tra điều kiện của khối Elif tiếp theo, v.v.Nếu tất cả các điều kiện là sai, cơ thể khác được thực thi.It allows us to check for multiple expressions. If the condition for if is False , it checks the condition of the next elif block and so on. If all the conditions are False , the body of else is executed.

Python có sử dụng Elif hay không nếu?

Elif khác là những câu lệnh có điều kiện cung cấp cho bạn việc đưa ra quyết định được yêu cầu khi bạn muốn thực thi mã dựa trên một điều kiện cụ thể.Tuyên bố IF IF Elif khác được sử dụng trong Python giúp tự động hóa quá trình ra quyết định đó.if… elif…else statement used in Python helps automate that decision making process.

Tại sao chúng ta sử dụng Elif trong Python?

Từ khóa Elif là cách nói của Pythons "Nếu các điều kiện trước đó không đúng, thì hãy thử điều kiện này".if the previous conditions were not true, then try this condition".

Tại sao khác không làm việc trong Python?

Nếu if-satatement là đúng, phần mã trong mệnh đề khác không chạy.Từ khóa khác cần phải ở trên dòng riêng của nó và ở cùng cấp độ thụt với từ khóa nếu khác tương ứng.Từ khóa khác cần được theo sau bởi một dấu hai chấm:.