Hướng dẫn what is the difference between if/else and cascaded if statements in python? - sự khác biệt giữa câu lệnh if/else và xếp tầng if trong python là gì?

Các câu lệnh if và if/other tản nhiệt kiểm tra một điều kiện duy nhất. Đó là hầu hết thời gian. Nhưng đôi khi chúng ta có một loạt các điều kiện mà chúng ta cần kiểm tra lẫn nhau. Hãy cùng xem cách chúng ta mã hóa trong Python.

Trong bài viết này:

  • Đánh giá một số điều kiện liên tiếp: Cascaded IFS
    • Mô hình mặc định của Python từ Cascaded If Statement
    • Các tính năng của câu lệnh Cascaded nếu
  • Các chương trình Python sử dụng các câu lệnh Cascaded nếu
    • Ví dụ: Quy trình đầu vào người dùng với câu lệnh CASCADED IF
    • Ví dụ: Xử lý các dự đoán với câu lệnh if cascaded
    • Ví dụ: Đếm các ký tự chuỗi có một câu lệnh if cascaded
  • Mẹo: Sử dụng xếp tầng nếu chỉ khi một bài kiểm tra phải vượt qua
  • Các loại python khác nếu câu lệnh
  • Bản tóm tắt

# Đánh giá một số điều kiện liên tiếp: Cascaded IFS

Một câu lệnh được xếp tầng nếu câu lệnh đánh giá một loạt các điều kiện cho đến khi một trong số chúng kiểm tra

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3. Khi Python gặp điều kiện
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3, nó sẽ thực hiện mã đi kèm. Sau đó xếp tầng nếu tuyên bố kết thúc (Python Docs, n.d.).cascaded if statement evaluates a series of conditions until one of them tests
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3. When Python comes across a
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3 condition, it executes its accompanying code. Then the cascaded if statement ends (Python Docs, n.d.).

Bằng cách đó, chúng tôi chỉ chọn một trong một số tùy chọn. Bạn cũng có thể biết câu lệnh IF theo các tên khác của nó: loạt các câu lệnh IF, if-else-if, chi nhánh nhiều chiều và if-elif-ladder. Nhưng bất kể tên của nó là gì, ý tưởng vẫn giữ nguyên: kiểm tra một số điều kiện cho đến khi chúng ta tìm thấy một điều kiện mà ____ ____. Hãy cùng xem cách chúng ta mã hóa một.

# Mô hình mặc định của Python từ Cascaded If Statement

Độ dài của một tầng nếu tuyên bố phụ thuộc vào số lượng nó có. Ở đây, những gì mẫu mặc định trông với 3 điều kiện (Python Docs, n.d.):

if condition1:
    # Code to execute when 'condition1' is true
elif condition2:
    # Code that runs when 'condition1' is false
    # and 'condition2' is true
elif condition3:
    # Code that executes when 'condition1' and 'condition2'
    # are false, and 'condition3' is true
[else:
    # Code that runs when all previously tested conditions are false]

Một tầng nếu câu lệnh kiểm tra các điều kiện cho đến khi một là

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3 (Python Docs, n.d.). Vì vậy, nếu
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
7 là
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3, mã của nó sẽ chạy và câu lệnh CASCADED IF kết thúc. Khi điều kiện đó là
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
9, Python chuyển sang
How many words did you write? 10
A quick comment; helpful too!
0. Nếu cái đó là
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3, mã đi kèm sẽ thực thi (và sau đó là câu lệnh được xếp tầng sẽ dừng lại).

Nếu điều kiện đó cũng là

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
9, các thử nghiệm Python
How many words did you write? 10
A quick comment; helpful too!
3. Khi
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3, mã chúng tôi đã đặt theo
How many words did you write? 10
A quick comment; helpful too!
5 chạy. Và khi
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
9, câu lệnh CASCADED IF kết thúc hoặc mã
How many words did you write? 10
A quick comment; helpful too!
7 tùy chọn thực thi.

Vì vậy, có ba phần chính cho một câu lệnh if cascaded (Matthes, 2016; Python Docs, N.D .; Sweigart, 2015):

  • Từ khóa
    How many words did you write? 10
    A quick comment; helpful too!
    
    8 theo sau là thử nghiệm có điều kiện đầu tiên.
  • Một hoặc nhiều từ khóa
    How many words did you write? 10
    A quick comment; helpful too!
    
    5 kiểm tra các điều kiện bổ sung.
  • Và một điều khoản
    How many words did you write? 10
    A quick comment; helpful too!
    
    7 tùy chọn chạy khi tất cả các điều kiện là
    wordsWritten = int(input("How many words did you write? "))
    
    if wordsWritten > 80000:
        print("That's a novel. Well done!")
    elif wordsWritten > 35000:
        print("That's an ebook. Nice!")
    elif wordsWritten > 15000:
        print("That's an in-depth paper. Good!")
    elif wordsWritten > 2000:
        print("Nice blog post length.")
    elif wordsWritten > 500:
        print("That's a nice FAQ entry.")
    elif wordsWritten > 200:
        print("And yet another email done.")
    else:
        print("A quick comment; helpful too!")
    
    9.

Đặc điểm xác định của câu lệnh Cascaded IF là Python thực thi một, và chính xác là một, khối mã (Python Docs, n.d.). Đó có thể là điều kiện đầu tiên chúng tôi kiểm tra với

How many words did you write? 10
A quick comment; helpful too!
8, một trong các kiểm tra
How many words did you write? 10
A quick comment; helpful too!
5 hoặc mệnh đề
How many words did you write? 10
A quick comment; helpful too!
7 cuối cùng. (Nếu chương trình của chúng tôi thực thi mã cho mỗi điều kiện
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3, thì chúng tôi cần sử dụng các câu lệnh riêng lẻ.)or the final
How many words did you write? 10
A quick comment; helpful too!
7 clause. (Should our program execute code for each
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3 condition, then we need to use individual if statements.)

# Các tính năng của câu lệnh CASCADED IF

Hành vi của Python sườn xếp tầng nếu câu lệnh khác nhau một chút dựa trên mệnh đề

How many words did you write? 10
A quick comment; helpful too!
7 cuối cùng:

  • Nếu không có tùy chọn
    How many words did you write? 10
    A quick comment; helpful too!
    
    7, mỗi khối mã phải vượt qua thử nghiệm
    How many words did you write? 10
    A quick comment; helpful too!
    
    8 hoặc
    How many words did you write? 10
    A quick comment; helpful too!
    
    5 cụ thể trước khi nó chạy. Khi tất cả các bài kiểm tra đó là
    wordsWritten = int(input("How many words did you write? "))
    
    if wordsWritten > 80000:
        print("That's a novel. Well done!")
    elif wordsWritten > 35000:
        print("That's an ebook. Nice!")
    elif wordsWritten > 15000:
        print("That's an in-depth paper. Good!")
    elif wordsWritten > 2000:
        print("Nice blog post length.")
    elif wordsWritten > 500:
        print("That's a nice FAQ entry.")
    elif wordsWritten > 200:
        print("And yet another email done.")
    else:
        print("A quick comment; helpful too!")
    
    9, không có gì của câu lệnh được thực hiện.
  • Với
    How many words did you write? 10
    A quick comment; helpful too!
    
    7, chúng ta có thể chắc chắn rằng ít nhất một cái gì đó của câu lệnh được thực hiện. Rốt cuộc, mã
    How many words did you write? 10
    A quick comment; helpful too!
    
    7 đó chạy khi tất cả các thử nghiệm là
    wordsWritten = int(input("How many words did you write? "))
    
    if wordsWritten > 80000:
        print("That's a novel. Well done!")
    elif wordsWritten > 35000:
        print("That's an ebook. Nice!")
    elif wordsWritten > 15000:
        print("That's an in-depth paper. Good!")
    elif wordsWritten > 2000:
        print("Nice blog post length.")
    elif wordsWritten > 500:
        print("That's a nice FAQ entry.")
    elif wordsWritten > 200:
        print("And yet another email done.")
    else:
        print("A quick comment; helpful too!")
    
    9.

Lưu ý rằng mã

How many words did you write? 10
A quick comment; helpful too!
7 của chúng tôi cuối cùng có thể chạy trong các tình huống mà chúng tôi đã không mong đợi khi chúng tôi viết mã. Điều đó có thể bao gồm dữ liệu bất ngờ, không hợp lệ hoặc thậm chí độc hại (Matthes, 2016).

Nếu bạn có một điều kiện cuối cùng cụ thể, thì hãy kết thúc câu lệnh CASCADED IF với bài kiểm tra

How many words did you write? 10
A quick comment; helpful too!
5 chứ không phải là điều khoản
How many words did you write? 10
A quick comment; helpful too!
7 chung. Điều đó cũng tạo ra mã rõ ràng và rõ ràng.

Một tính năng khác của câu lệnh Cascaded IF là thứ tự điều kiện quan trọng (Sweigart, 2015). Điều đó xảy ra bởi vì khi một điều kiện là

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3, những người khác bị bỏ qua. Vì vậy, các thử nghiệm có điều kiện cao hơn các câu lệnh nếu câu lệnh thường có nhiều khả năng chạy hơn. Mã xuất hiện sau này trong câu lệnh Cascaded IF, mặt khác, ít có khả năng chạy hơn.

Các tính năng chung của câu lệnh CASCADED IF là:

  • Không có giới hạn về số lượng bài kiểm tra
    How many words did you write? 10
    A quick comment; helpful too!
    
    5 chúng ta có thể làm; Cascaded của chúng tôi nếu tuyên bố có thể trở thành miễn là cần thiết.
  • Mỗi dòng
    How many words did you write? 10
    A quick comment; helpful too!
    
    8,
    How many words did you write? 10
    A quick comment; helpful too!
    
    5 và
    How many words did you write? 10
    A quick comment; helpful too!
    
    7 kết thúc bằng một dấu hai chấm (
    print("Guess the number of pages in "
          "'Harry Potter and the Order of the Phoenix'.")
    
    while True:
        guess = int(input("Your guess?  "))
    
        if guess == 766:
            print("Correct! Well done!")
            break
        elif guess > 1000:
            print("Way too high! Guess lower.")
        elif guess < 500:
            print("The book has much more pages. Guess higher.")
        elif guess > 766 and guess < 850:
            print("You're close. But guess lower.")
        elif guess > 675 and guess < 766:
            print("You're close. Guess a bit higher.")
        else:
            print("Incorrect, give it another guess.")
    
    2).
  • Các từ khóa
    How many words did you write? 10
    A quick comment; helpful too!
    
    8,
    How many words did you write? 10
    A quick comment; helpful too!
    
    5 và
    How many words did you write? 10
    A quick comment; helpful too!
    
    7 đều sử dụng cùng một cấp độ thụt. Đó là cách mà Python biết họ thuộc về nhau (Lutz, 2013).

# Các chương trình Python sử dụng các câu lệnh Cascaded nếu

Bây giờ chúng ta đã biết câu lệnh CASCADED IF, hãy để xem cách chúng ta sử dụng nó với một vài chương trình ví dụ Python.

# Ví dụ: Quy trình đầu vào người dùng với câu lệnh CASCADED IF

Rất nhiều câu lệnh được xếp tầng nếu các câu lệnh tương tự: đánh giá một biến so với nhiều giá trị. (Điều đó tương tự như các câu lệnh chuyển đổi trong các ngôn ngữ lập trình khác.) Chương trình ví dụ tạo ra một trong những câu lệnh IF được xếp tầng.

Mã trước tiên nhắc nhở người dùng về số lượng từ anh ấy hoặc cô ấy đã viết. Sau đó, chúng tôi so sánh số lượng đó so với độ dài của một số loại nội dung.

Mã chương trình là:

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")

Đầu tiên chúng tôi thực hiện biến

print("Guess the number of pages in "
      "'Harry Potter and the Order of the Phoenix'.")

while True:
    guess = int(input("Your guess?  "))

    if guess == 766:
        print("Correct! Well done!")
        break
    elif guess > 1000:
        print("Way too high! Guess lower.")
    elif guess < 500:
        print("The book has much more pages. Guess higher.")
    elif guess > 766 and guess < 850:
        print("You're close. But guess lower.")
    elif guess > 675 and guess < 766:
        print("You're close. Guess a bit higher.")
    else:
        print("Incorrect, give it another guess.")
6. Chúng tôi đặt biến đó thành giá trị được trả về bởi hàm
print("Guess the number of pages in "
      "'Harry Potter and the Order of the Phoenix'.")

while True:
    guess = int(input("Your guess?  "))

    if guess == 766:
        print("Correct! Well done!")
        break
    elif guess > 1000:
        print("Way too high! Guess lower.")
    elif guess < 500:
        print("The book has much more pages. Guess higher.")
    elif guess > 766 and guess < 850:
        print("You're close. But guess lower.")
    elif guess > 675 and guess < 766:
        print("You're close. Guess a bit higher.")
    else:
        print("Incorrect, give it another guess.")
7 mà chúng tôi chuyển đổi thành một số nguyên (
print("Guess the number of pages in "
      "'Harry Potter and the Order of the Phoenix'.")

while True:
    guess = int(input("Your guess?  "))

    if guess == 766:
        print("Correct! Well done!")
        break
    elif guess > 1000:
        print("Way too high! Guess lower.")
    elif guess < 500:
        print("The book has much more pages. Guess higher.")
    elif guess > 766 and guess < 850:
        print("You're close. But guess lower.")
    elif guess > 675 and guess < 766:
        print("You're close. Guess a bit higher.")
    else:
        print("Incorrect, give it another guess.")
8). Với chức năng
print("Guess the number of pages in "
      "'Harry Potter and the Order of the Phoenix'.")

while True:
    guess = int(input("Your guess?  "))

    if guess == 766:
        print("Correct! Well done!")
        break
    elif guess > 1000:
        print("Way too high! Guess lower.")
    elif guess < 500:
        print("The book has much more pages. Guess higher.")
    elif guess > 766 and guess < 850:
        print("You're close. But guess lower.")
    elif guess > 675 and guess < 766:
        print("You're close. Guess a bit higher.")
    else:
        print("Incorrect, give it another guess.")
7 đó, chúng tôi hỏi người dùng có bao nhiêu từ anh ấy hoặc cô ấy đã viết.

Sau đó, chúng tôi đánh giá biến đó với một câu lệnh if cascaded. Chúng tôi thực hiện một số so sánh:

  • Trước tiên chúng tôi xem nếu số từ trên 80.000 (
    Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
    Your guess?  1200
    Way too high! Guess lower.
    Your guess?  400
    The book has much more pages. Guess higher.
    Your guess?  800
    You're close. But guess lower.
    Your guess?  700
    You're close. Guess a bit higher.
    Your guess?  750
    You're close. Guess a bit higher.
    Your guess?  766
    Correct! Well done!
    
    0). Khi có, hàm
    Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
    Your guess?  1200
    Way too high! Guess lower.
    Your guess?  400
    The book has much more pages. Guess higher.
    Your guess?  800
    You're close. But guess lower.
    Your guess?  700
    You're close. Guess a bit higher.
    Your guess?  750
    You're close. Guess a bit higher.
    Your guess?  766
    Correct! Well done!
    
    1 nói rằng độ dài của một cuốn tiểu thuyết.
  • Thử nghiệm có điều kiện thứ hai chạy khi số từ không ở trên 80k. Thử nghiệm này trông nếu biến ở trên
    Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
    Your guess?  1200
    Way too high! Guess lower.
    Your guess?  400
    The book has much more pages. Guess higher.
    Your guess?  800
    You're close. But guess lower.
    Your guess?  700
    You're close. Guess a bit higher.
    Your guess?  750
    You're close. Guess a bit higher.
    Your guess?  766
    Correct! Well done!
    
    2. Khi có, chúng ta biết số từ dưới 80.000 (vì thử nghiệm đầu tiên là
    wordsWritten = int(input("How many words did you write? "))
    
    if wordsWritten > 80000:
        print("That's a novel. Well done!")
    elif wordsWritten > 35000:
        print("That's an ebook. Nice!")
    elif wordsWritten > 15000:
        print("That's an in-depth paper. Good!")
    elif wordsWritten > 2000:
        print("Nice blog post length.")
    elif wordsWritten > 500:
        print("That's a nice FAQ entry.")
    elif wordsWritten > 200:
        print("And yet another email done.")
    else:
        print("A quick comment; helpful too!")
    
    9) nhưng hơn 35.000 (vì thử nghiệm thứ hai này là
    wordsWritten = int(input("How many words did you write? "))
    
    if wordsWritten > 80000:
        print("That's a novel. Well done!")
    elif wordsWritten > 35000:
        print("That's an ebook. Nice!")
    elif wordsWritten > 15000:
        print("That's an in-depth paper. Good!")
    elif wordsWritten > 2000:
        print("Nice blog post length.")
    elif wordsWritten > 500:
        print("That's a nice FAQ entry.")
    elif wordsWritten > 200:
        print("And yet another email done.")
    else:
        print("A quick comment; helpful too!")
    
    3). Chúng tôi đã xem xét rằng một ebook.
  • Thử nghiệm thứ ba có vẻ nếu
    print("Guess the number of pages in "
          "'Harry Potter and the Order of the Phoenix'.")
    
    while True:
        guess = int(input("Your guess?  "))
    
        if guess == 766:
            print("Correct! Well done!")
            break
        elif guess > 1000:
            print("Way too high! Guess lower.")
        elif guess < 500:
            print("The book has much more pages. Guess higher.")
        elif guess > 766 and guess < 850:
            print("You're close. But guess lower.")
        elif guess > 675 and guess < 766:
            print("You're close. Guess a bit higher.")
        else:
            print("Incorrect, give it another guess.")
    
    6 ở trên (
    Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
    Your guess?  1200
    Way too high! Guess lower.
    Your guess?  400
    The book has much more pages. Guess higher.
    Your guess?  800
    You're close. But guess lower.
    Your guess?  700
    You're close. Guess a bit higher.
    Your guess?  750
    You're close. Guess a bit higher.
    Your guess?  766
    Correct! Well done!
    
    7) 15.000. Nếu bài kiểm tra này
    wordsWritten = int(input("How many words did you write? "))
    
    if wordsWritten > 80000:
        print("That's a novel. Well done!")
    elif wordsWritten > 35000:
        print("That's an ebook. Nice!")
    elif wordsWritten > 15000:
        print("That's an in-depth paper. Good!")
    elif wordsWritten > 2000:
        print("Nice blog post length.")
    elif wordsWritten > 500:
        print("That's a nice FAQ entry.")
    elif wordsWritten > 200:
        print("And yet another email done.")
    else:
        print("A quick comment; helpful too!")
    
    3, thì chúng ta cũng có thể suy luận rằng các từ dưới 35.000. Vì vậy,
    Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
    Your guess?  1200
    Way too high! Guess lower.
    Your guess?  400
    The book has much more pages. Guess higher.
    Your guess?  800
    You're close. But guess lower.
    Your guess?  700
    You're close. Guess a bit higher.
    Your guess?  750
    You're close. Guess a bit higher.
    Your guess?  766
    Correct! Well done!
    
    1 xem xét rằng một bài nghiên cứu.
  • Thử nghiệm thứ tư nhìn thấy nếu số từ là trên 2k (
    exampleStr = ("Python is a general purpose programming "
                  "language build by a big open source community.")
    
    countE = 0
    countO = 0
    countP = 0
    
    for char in exampleStr:
        if char.lower() == 'e':
            countE = countE + 1
        elif char.lower() == 'o':
            countO = countO + 1
        elif char.lower() == 'p':
            countP = countP + 1
    
    print('"' + exampleStr + '"')
    print("The string has", countE, "'e' letters,",
          countO, "'o' letters, and", countP, "'p' characters.")
    
    0). Đó sẽ là một chiều dài blog tốt. Nếu các từ aren trên mức đó, chúng tôi sẽ chuyển sang mệnh đề
    How many words did you write? 10
    A quick comment; helpful too!
    
    5 tiếp theo.
  • So sánh thứ năm có vẻ nếu có hơn 500 từ (
    exampleStr = ("Python is a general purpose programming "
                  "language build by a big open source community.")
    
    countE = 0
    countO = 0
    countP = 0
    
    for char in exampleStr:
        if char.lower() == 'e':
            countE = countE + 1
        elif char.lower() == 'o':
            countO = countO + 1
        elif char.lower() == 'p':
            countP = countP + 1
    
    print('"' + exampleStr + '"')
    print("The string has", countE, "'e' letters,",
          countO, "'o' letters, and", countP, "'p' characters.")
    
    2). Điều đó làm cho một bài viết Câu hỏi thường gặp ngắn. Khi số lượng không phải là trên mức đó, chúng tôi sẽ tiếp tục.
  • Các thử nghiệm mệnh đề
    How many words did you write? 10
    A quick comment; helpful too!
    
    5 cuối cùng
    exampleStr = ("Python is a general purpose programming "
                  "language build by a big open source community.")
    
    countE = 0
    countO = 0
    countP = 0
    
    for char in exampleStr:
        if char.lower() == 'e':
            countE = countE + 1
        elif char.lower() == 'o':
            countO = countO + 1
        elif char.lower() == 'p':
            countP = countP + 1
    
    print('"' + exampleStr + '"')
    print("The string has", countE, "'e' letters,",
          countO, "'o' letters, and", countP, "'p' characters.")
    
    4. Khi đây là
    wordsWritten = int(input("How many words did you write? "))
    
    if wordsWritten > 80000:
        print("That's a novel. Well done!")
    elif wordsWritten > 35000:
        print("That's an ebook. Nice!")
    elif wordsWritten > 15000:
        print("That's an in-depth paper. Good!")
    elif wordsWritten > 2000:
        print("Nice blog post length.")
    elif wordsWritten > 500:
        print("That's a nice FAQ entry.")
    elif wordsWritten > 200:
        print("And yet another email done.")
    else:
        print("A quick comment; helpful too!")
    
    3, hàm
    Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
    Your guess?  1200
    Way too high! Guess lower.
    Your guess?  400
    The book has much more pages. Guess higher.
    Your guess?  800
    You're close. But guess lower.
    Your guess?  700
    You're close. Guess a bit higher.
    Your guess?  750
    You're close. Guess a bit higher.
    Your guess?  766
    Correct! Well done!
    
    1 xem xét phạm vi 201-500 từ một email.
  • Điều khoản
    How many words did you write? 10
    A quick comment; helpful too!
    
    7 chạy khi không có điều kiện nào trước đây là
    wordsWritten = int(input("How many words did you write? "))
    
    if wordsWritten > 80000:
        print("That's a novel. Well done!")
    elif wordsWritten > 35000:
        print("That's an ebook. Nice!")
    elif wordsWritten > 15000:
        print("That's an in-depth paper. Good!")
    elif wordsWritten > 2000:
        print("Nice blog post length.")
    elif wordsWritten > 500:
        print("That's a nice FAQ entry.")
    elif wordsWritten > 200:
        print("And yet another email done.")
    else:
        print("A quick comment; helpful too!")
    
    3. Trong trường hợp của chúng tôi, điều đó có nghĩa là các từ được viết là 200 hoặc ít hơn. Chúng tôi xem xét rằng một bình luận.

Tất nhiên các đầu ra chương trình phụ thuộc vào số lượng từ được cung cấp. Dưới đây là một số ví dụ đầu ra:

How many words did you write? 10
A quick comment; helpful too!
How many words did you write? 2982
Nice blog post length.
How many words did you write? 34567
That's an in-depth paper. Good!

# Ví dụ: Xử lý các dự đoán với câu lệnh if cascaded

Trong chương trình Python dưới đây, chúng tôi hỏi người dùng có bao nhiêu trang ‘Harry Potter và thứ tự của cuốn sách Phoenix. Với một câu lệnh IF được xếp tầng, chúng tôi đánh giá dự đoán của người dùng và cung cấp một số gợi ý về câu trả lời đúng (766 trang).

Mã chương trình hoàn chỉnh là:

print("Guess the number of pages in "
      "'Harry Potter and the Order of the Phoenix'.")

while True:
    guess = int(input("Your guess?  "))

    if guess == 766:
        print("Correct! Well done!")
        break
    elif guess > 1000:
        print("Way too high! Guess lower.")
    elif guess < 500:
        print("The book has much more pages. Guess higher.")
    elif guess > 766 and guess < 850:
        print("You're close. But guess lower.")
    elif guess > 675 and guess < 766:
        print("You're close. Guess a bit higher.")
    else:
        print("Incorrect, give it another guess.")

Trước tiên chúng tôi có chức năng

Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
Your guess?  1200
Way too high! Guess lower.
Your guess?  400
The book has much more pages. Guess higher.
Your guess?  800
You're close. But guess lower.
Your guess?  700
You're close. Guess a bit higher.
Your guess?  750
You're close. Guess a bit higher.
Your guess?  766
Correct! Well done!
1 nói mục đích của chương trình. Sau đó, chúng tôi tạo ra một vòng lặp
"Python is a general purpose programming language build by a big open source community."
The string has 6 'e' letters, 6 'o' letters, and 5 'p' characters.
0 vô hạn. Điều đó giữ cho chương trình của chúng tôi chạy cho đến khi người dùng đoán đúng (tất nhiên là không thân thiện với người dùng!).

Bên trong vòng lặp, chúng tôi tạo biến

"Python is a general purpose programming language build by a big open source community."
The string has 6 'e' letters, 6 'o' letters, and 5 'p' characters.
1. Chúng tôi đặt giá trị của nó thành hàm
print("Guess the number of pages in "
      "'Harry Potter and the Order of the Phoenix'.")

while True:
    guess = int(input("Your guess?  "))

    if guess == 766:
        print("Correct! Well done!")
        break
    elif guess > 1000:
        print("Way too high! Guess lower.")
    elif guess < 500:
        print("The book has much more pages. Guess higher.")
    elif guess > 766 and guess < 850:
        print("You're close. But guess lower.")
    elif guess > 675 and guess < 766:
        print("You're close. Guess a bit higher.")
    else:
        print("Incorrect, give it another guess.")
7, được chuyển đổi thành số nguyên (
print("Guess the number of pages in "
      "'Harry Potter and the Order of the Phoenix'.")

while True:
    guess = int(input("Your guess?  "))

    if guess == 766:
        print("Correct! Well done!")
        break
    elif guess > 1000:
        print("Way too high! Guess lower.")
    elif guess < 500:
        print("The book has much more pages. Guess higher.")
    elif guess > 766 and guess < 850:
        print("You're close. But guess lower.")
    elif guess > 675 and guess < 766:
        print("You're close. Guess a bit higher.")
    else:
        print("Incorrect, give it another guess.")
8). Với chức năng
print("Guess the number of pages in "
      "'Harry Potter and the Order of the Phoenix'.")

while True:
    guess = int(input("Your guess?  "))

    if guess == 766:
        print("Correct! Well done!")
        break
    elif guess > 1000:
        print("Way too high! Guess lower.")
    elif guess < 500:
        print("The book has much more pages. Guess higher.")
    elif guess > 766 and guess < 850:
        print("You're close. But guess lower.")
    elif guess > 675 and guess < 766:
        print("You're close. Guess a bit higher.")
    else:
        print("Incorrect, give it another guess.")
7, hãy gọi, chúng tôi nhắc người dùng ước tính của mình.

Sau đó, chúng tôi xử lý ước tính đó với một câu lệnh if cascaded:

  • Điều kiện đầu tiên có vẻ nếu người dùng ước tính (
    "Python is a general purpose programming language build by a big open source community."
    The string has 6 'e' letters, 6 'o' letters, and 5 'p' characters.
    
    1) bằng (
    "Python is a general purpose programming language build by a big open source community."
    The string has 6 'e' letters, 6 'o' letters, and 5 'p' characters.
    
    6)
    "Python is a general purpose programming language build by a big open source community."
    The string has 6 'e' letters, 6 'o' letters, and 5 'p' characters.
    
    7. Khi nó làm, người dùng đã làm đúng. Chúng tôi chúc mừng họ với chức năng
    Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
    Your guess?  1200
    Way too high! Guess lower.
    Your guess?  400
    The book has much more pages. Guess higher.
    Your guess?  800
    You're close. But guess lower.
    Your guess?  700
    You're close. Guess a bit higher.
    Your guess?  750
    You're close. Guess a bit higher.
    Your guess?  766
    Correct! Well done!
    
    1 và sau đó dừng vòng lặp bằng câu lệnh
    "Python is a general purpose programming language build by a big open source community."
    The string has 6 'e' letters, 6 'o' letters, and 5 'p' characters.
    
    9. Nếu
    "Python is a general purpose programming language build by a big open source community."
    The string has 6 'e' letters, 6 'o' letters, and 5 'p' characters.
    
    1 có một giá trị khác, chúng tôi sẽ tiếp tục với câu lệnh if statement mã khác.
  • Điều kiện tiếp theo có vẻ nếu dự đoán ở trên (
    Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
    Your guess?  1200
    Way too high! Guess lower.
    Your guess?  400
    The book has much more pages. Guess higher.
    Your guess?  800
    You're close. But guess lower.
    Your guess?  700
    You're close. Guess a bit higher.
    Your guess?  750
    You're close. Guess a bit higher.
    Your guess?  766
    Correct! Well done!
    
    7) 1.000. Trong trường hợp đó, chúng tôi có
    Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
    Your guess?  1200
    Way too high! Guess lower.
    Your guess?  400
    The book has much more pages. Guess higher.
    Your guess?  800
    You're close. But guess lower.
    Your guess?  700
    You're close. Guess a bit higher.
    Your guess?  750
    You're close. Guess a bit higher.
    Your guess?  766
    Correct! Well done!
    
    1 nói rằng dự đoán là quá cao.
  • Điều khoản tiếp theo cho thấy nếu dự đoán nằm dưới (____993) 500. Đó cũng là cách, vì vậy
    Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
    Your guess?  1200
    Way too high! Guess lower.
    Your guess?  400
    The book has much more pages. Guess higher.
    Your guess?  800
    You're close. But guess lower.
    Your guess?  700
    You're close. Guess a bit higher.
    Your guess?  750
    You're close. Guess a bit higher.
    Your guess?  766
    Correct! Well done!
    
    1 đưa ra một gợi ý khác.
  • Sau đó, chúng tôi đánh giá xem liệu người dùng dự đoán có phải là từ 767-849 trang (
    requestedExtras = ['Coke', 'Ketchup', 'French fries']
    
    if 'Coke' in requestedExtras:
        print('Include an extra large serving of Coca Cola')
    elif 'Ketchup' in requestedExtras:
        print('Add some extra ketchup')
    elif 'French fries' in requestedExtras:
        print('A bit more French fries, please!')
    
    5). Khi dự đoán rơi vào phạm vi đó, người dùng sẽ gần. Nhưng số lượng trang chính xác vẫn còn dưới đó, vì vậy
    Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
    Your guess?  1200
    Way too high! Guess lower.
    Your guess?  400
    The book has much more pages. Guess higher.
    Your guess?  800
    You're close. But guess lower.
    Your guess?  700
    You're close. Guess a bit higher.
    Your guess?  750
    You're close. Guess a bit higher.
    Your guess?  766
    Correct! Well done!
    
    1 nói như vậy.
  • Điều khoản
    How many words did you write? 10
    A quick comment; helpful too!
    
    5 cuối cùng cho thấy nếu ước tính là từ 676-765 trang (
    requestedExtras = ['Coke', 'Ketchup', 'French fries']
    
    if 'Coke' in requestedExtras:
        print('Include an extra large serving of Coca Cola')
    elif 'Ketchup' in requestedExtras:
        print('Add some extra ketchup')
    elif 'French fries' in requestedExtras:
        print('A bit more French fries, please!')
    
    8). Đó cũng là một phỏng đoán gần, nhưng hơi quá cao. Vì vậy,
    Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
    Your guess?  1200
    Way too high! Guess lower.
    Your guess?  400
    The book has much more pages. Guess higher.
    Your guess?  800
    You're close. But guess lower.
    Your guess?  700
    You're close. Guess a bit higher.
    Your guess?  750
    You're close. Guess a bit higher.
    Your guess?  766
    Correct! Well done!
    
    1 đưa ra một gợi ý khác.
  • Mệnh đề
    How many words did you write? 10
    A quick comment; helpful too!
    
    7 cuối cùng thực hiện khi người dùng đoán là không phù hợp với bất kỳ điều kiện nào trước đó. Điều đó ví dụ xảy ra với dự đoán 550 hoặc 950. Trong các kịch bản đó
    Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
    Your guess?  1200
    Way too high! Guess lower.
    Your guess?  400
    The book has much more pages. Guess higher.
    Your guess?  800
    You're close. But guess lower.
    Your guess?  700
    You're close. Guess a bit higher.
    Your guess?  750
    You're close. Guess a bit higher.
    Your guess?  766
    Correct! Well done!
    
    1 chỉ đơn giản nói rằng dự đoán là sai.

Việc đầu ra chương trình tạo ra phụ thuộc vào dự đoán của người dùng. Ở đây, một ví dụ về đầu ra có thể có:

Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
Your guess?  1200
Way too high! Guess lower.
Your guess?  400
The book has much more pages. Guess higher.
Your guess?  800
You're close. But guess lower.
Your guess?  700
You're close. Guess a bit higher.
Your guess?  750
You're close. Guess a bit higher.
Your guess?  766
Correct! Well done!

# Ví dụ: Đếm các ký tự chuỗi với câu lệnh if cascaded

Tất nhiên có rất nhiều cách khác để sử dụng các câu lệnh ifcaded. Một ví dụ khác cho thấy dưới đây. Lần này, chúng tôi sẽ đếm số lượng chữ cái, và chữ P, và các chữ cái P, xuất hiện trong một câu. Mã chương trình là:

exampleStr = ("Python is a general purpose programming "
              "language build by a big open source community.")

countE = 0
countO = 0
countP = 0

for char in exampleStr:
    if char.lower() == 'e':
        countE = countE + 1
    elif char.lower() == 'o':
        countO = countO + 1
    elif char.lower() == 'p':
        countP = countP + 1

print('"' + exampleStr + '"')
print("The string has", countE, "'e' letters,",
      countO, "'o' letters, and", countP, "'p' characters.")

Đầu tiên chúng tôi thực hiện biến

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
02 và đặt nó thành một câu về Python. Sau đó, chúng tôi thực hiện ba biến số (
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
03,
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
04 và
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
05) mà chúng tôi sử dụng để đếm sau.

Tiếp theo chúng tôi tạo một vòng

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
06. Vòng lặp này đi qua tất cả các ký tự trong câu ví dụ của chúng tôi. Với mỗi chu kỳ vòng lặp, biến
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
07 giữ một ký tự duy nhất từ ​​chuỗi đó.

Để kiểm tra ký tự đơn đó mỗi lần thông qua vòng lặp, chúng tôi sử dụng câu lệnh IF được xếp tầng:

  • Đầu tiên chúng ta xem nếu ký tự trong chữ thường (
    wordsWritten = int(input("How many words did you write? "))
    
    if wordsWritten > 80000:
        print("That's a novel. Well done!")
    elif wordsWritten > 35000:
        print("That's an ebook. Nice!")
    elif wordsWritten > 15000:
        print("That's an in-depth paper. Good!")
    elif wordsWritten > 2000:
        print("Nice blog post length.")
    elif wordsWritten > 500:
        print("That's a nice FAQ entry.")
    elif wordsWritten > 200:
        print("And yet another email done.")
    else:
        print("A quick comment; helpful too!")
    
    08) bằng (
    "Python is a general purpose programming language build by a big open source community."
    The string has 6 'e' letters, 6 'o' letters, and 5 'p' characters.
    
    6) chữ ’e. Khi nó xảy ra, chúng tôi tăng biến
    wordsWritten = int(input("How many words did you write? "))
    
    if wordsWritten > 80000:
        print("That's a novel. Well done!")
    elif wordsWritten > 35000:
        print("That's an ebook. Nice!")
    elif wordsWritten > 15000:
        print("That's an in-depth paper. Good!")
    elif wordsWritten > 2000:
        print("Nice blog post length.")
    elif wordsWritten > 500:
        print("That's a nice FAQ entry.")
    elif wordsWritten > 200:
        print("And yet another email done.")
    else:
        print("A quick comment; helpful too!")
    
    03 với một. Nếu nhân vật không phải là một ’e, thì bài kiểm tra tiếp theo sẽ chạy.
  • Mà người ta nhìn thấy nếu ký tự trong chữ thường khớp (
    "Python is a general purpose programming language build by a big open source community."
    The string has 6 'e' letters, 6 'o' letters, and 5 'p' characters.
    
    6) chữ ’o. Khi nó thực hiện, chúng tôi tăng biến
    wordsWritten = int(input("How many words did you write? "))
    
    if wordsWritten > 80000:
        print("That's a novel. Well done!")
    elif wordsWritten > 35000:
        print("That's an ebook. Nice!")
    elif wordsWritten > 15000:
        print("That's an in-depth paper. Good!")
    elif wordsWritten > 2000:
        print("Nice blog post length.")
    elif wordsWritten > 500:
        print("That's a nice FAQ entry.")
    elif wordsWritten > 200:
        print("And yet another email done.")
    else:
        print("A quick comment; helpful too!")
    
    04 với một. Nếu không chúng tôi chuyển sang bài kiểm tra tiếp theo.
  • Thử nghiệm đó đánh giá nếu ký tự trong chữ thường bằng ‘p. Nếu có, chúng tôi sẽ tăng biến
    wordsWritten = int(input("How many words did you write? "))
    
    if wordsWritten > 80000:
        print("That's a novel. Well done!")
    elif wordsWritten > 35000:
        print("That's an ebook. Nice!")
    elif wordsWritten > 15000:
        print("That's an in-depth paper. Good!")
    elif wordsWritten > 2000:
        print("Nice blog post length.")
    elif wordsWritten > 500:
        print("That's a nice FAQ entry.")
    elif wordsWritten > 200:
        print("And yet another email done.")
    else:
        print("A quick comment; helpful too!")
    
    05 với một.

Lưu ý rằng câu lệnh này được xếp tầng nếu không có trường hợp mặc định. Vì chúng tôi tìm kiếm các ký tự cụ thể, chúng tôi không muốn thực thi mã cho bất kỳ ký tự nào khác. Chúng tôi chỉ quan tâm đến ’e,‘ o, và ‘p,.

Sau khi vòng lặp kết thúc

Guess the number of pages in 'Harry Potter and the Order of the Phoenix'.
Your guess?  1200
Way too high! Guess lower.
Your guess?  400
The book has much more pages. Guess higher.
Your guess?  800
You're close. But guess lower.
Your guess?  700
You're close. Guess a bit higher.
Your guess?  750
You're close. Guess a bit higher.
Your guess?  766
Correct! Well done!
1 xuất ra câu ví dụ. Sau đó, chúng tôi in các giá trị của các biến
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
03,
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
04 và
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
05. Điều đó cho thấy có bao nhiêu ’e,’ o, và ‘p, chúng tôi đã tìm thấy:

"Python is a general purpose programming language build by a big open source community."
The string has 6 'e' letters, 6 'o' letters, and 5 'p' characters.

# Mẹo: Sử dụng Cascaded nếu chỉ khi một bài kiểm tra phải vượt qua

Một bản xếp tầng nếu câu lệnh thực thi nhiều nhất chỉ là một mệnh đề. Chỉ có điều kiện

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3 đầu tiên có mã được thực thi. Ngay cả khi nhiều điều kiện là
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3, nó chỉ là điều đầu tiên thực hiện. Sau đó, các tầng nếu tuyên bố chỉ đơn giản là kết thúc.

Hành vi đó có một hậu quả quan trọng. Khi nhiều điều kiện trong chương trình của chúng tôi có thể là

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3 cùng một lúc và mã của chúng tôi sẽ hoạt động theo từng điều kiện
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3, thì chúng tôi không nên sử dụng câu lệnh CASCADED IF. Thay vào đó, chúng tôi cần nhiều câu lệnh IF độc lập (Matthes, 2016). Bằng cách đó chúng ta có thể đánh giá từng điều kiện một cách độc lập.and our code should act on each
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3 condition, then we shouldn’t use a cascaded if statement. Instead we need multiple independent if statements (Matthes, 2016). That way we can evaluate each condition independently.

Hãy cùng nhìn vào một ví dụ. Giả sử ứng dụng của chúng tôi xử lý các đơn đặt hàng trong một nhà hàng nhanh. Chúng ta có thể có một đoạn mã như thế này:

requestedExtras = ['Coke', 'Ketchup', 'French fries']

if 'Coke' in requestedExtras:
    print('Include an extra large serving of Coca Cola')
elif 'Ketchup' in requestedExtras:
    print('Add some extra ketchup')
elif 'French fries' in requestedExtras:
    print('A bit more French fries, please!')

Ở đây chúng tôi có một danh sách các tính năng bổ sung mà khách hàng đặt hàng (

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
22). Một bản xếp tầng nếu câu lệnh sau đó trông giống như khách hàng đã đặt hàng.

Nhưng ở đây, điều này. Khách hàng cụ thể này đã đặt hàng ba tính năng bổ sung. Nhưng câu lệnh CASCADED NẾU chỉ xuất ra các hướng dẫn của nhân viên cho một:

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
0

Trong chương trình này, nhiều điều kiện của câu lệnh CASCADED IF có thể là

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3 cùng một lúc. Khi điều đó xảy ra, chúng tôi muốn chạy mã cho mỗi điều kiện
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3. Vì vậy, chúng tôi không cần một bản sao nếu tuyên bố; Chúng tôi yêu cầu cá nhân nếu câu lệnh thay thế. Như thế này:each
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3 condition. So we don’t need a cascaded if statement; we require individual if statements instead. Like this:

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
1

Ở đây chúng tôi đã thay thế các điều khoản

How many words did you write? 10
A quick comment; helpful too!
5 bằng các bài kiểm tra
How many words did you write? 10
A quick comment; helpful too!
8 thích hợp. Điều đó làm cho mỗi người trong số họ độc lập với mã trước đó. Bây giờ khi khách hàng đặt hàng ba tính năng bổ sung khác nhau, anh ta hoặc cô ta thực sự có ba phần bổ sung. Ở đây, những gì mã bây giờ xuất ra:

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
2

# Các loại python khác nếu câu lệnh

Bên cạnh tuyên bố theo tầng nếu Python còn có một vài tuyên bố quyết định khác:

  • Câu lệnh IF đánh giá một điều kiện, khi
    wordsWritten = int(input("How many words did you write? "))
    
    if wordsWritten > 80000:
        print("That's a novel. Well done!")
    elif wordsWritten > 35000:
        print("That's an ebook. Nice!")
    elif wordsWritten > 15000:
        print("That's an in-depth paper. Good!")
    elif wordsWritten > 2000:
        print("Nice blog post length.")
    elif wordsWritten > 500:
        print("That's a nice FAQ entry.")
    elif wordsWritten > 200:
        print("And yet another email done.")
    else:
        print("A quick comment; helpful too!")
    
    3, làm cho chương trình thực thi một số mã.
  • Một câu lệnh nếu được tuyên bố là một tuyên bố nếu được đặt bên trong một câu khác. Điều đó làm cho mã
    How many words did you write? 10
    A quick comment; helpful too!
    
    8 chạy phụ thuộc vào các điều kiện khác, đó là cách chúng ta có thể xử lý các tình huống phức tạp.
  • Một câu lệnh if/other chọn giữa một trong hai đường dẫn mã dựa trên so sánh đúng/sai.
  • Và một câu lệnh nếu/khác đặt mã nếu/khác mã bên trong câu lệnh IF khác. Theo cách đó, mã if/other chạy sau khi mã đã kiểm tra một số điều kiện khác.

Xem tất cả các bài viết trong danh mục IF để biết nhiều hơn về các câu lệnh của Python.

# Bản tóm tắt

Một câu lệnh IF hoặc IF/ELSE đánh giá một điều kiện duy nhất. Để kiểm tra nhiều điều kiện với nhau, chúng tôi sử dụng các mệnh đề

How many words did you write? 10
A quick comment; helpful too!
5 bổ sung. Điều đó tạo ra những gì được gọi là một câu lệnh if cascaded.

Với một bản xếp tầng nếu câu lệnh Python đánh giá các điều kiện cho đến khi một trong số chúng là

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3. Mã của mệnh đề đó sau đó thực thi, và sau đó câu lệnh CASCADED IF kết thúc. Ngay cả khi một số điều kiện là
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3, Python vẫn chỉ chạy mã của bài kiểm tra
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3 đầu tiên.

Có hai tùy chọn khi tất cả các điều kiện kiểm tra

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
9. Điều khoản cuối cùng và tùy chọn
How many words did you write? 10
A quick comment; helpful too!
7 thực thi. Hoặc không có mã mặc định đó, không có gì của câu lệnh CASCADED chạy.

Vì Python chỉ chạy điều kiện

wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
3 đầu tiên, các điều kiện cao hơn trong câu lệnh Cascaded nếu có nhiều khả năng chạy. Mã thấp hơn trong câu lệnh CASCADED NẾU ít có khả năng thực thi. Những người yêu cầu sau tất cả các điều kiện trước đó là
wordsWritten = int(input("How many words did you write? "))

if wordsWritten > 80000:
    print("That's a novel. Well done!")
elif wordsWritten > 35000:
    print("That's an ebook. Nice!")
elif wordsWritten > 15000:
    print("That's an in-depth paper. Good!")
elif wordsWritten > 2000:
    print("Nice blog post length.")
elif wordsWritten > 500:
    print("That's a nice FAQ entry.")
elif wordsWritten > 200:
    print("And yet another email done.")
else:
    print("A quick comment; helpful too!")
9.

Người giới thiệu

Lutz, M. (2013). Học Python (Phiên bản thứ 5). Sebastopol, CA: O hèReilly Media.

Matthes, E. (2016). Python Crash Course: Một phần giới thiệu thực hành, dựa trên dự án về lập trình. San Francisco, CA: Không có báo chí tinh bột.

Python.org (N.D.). Báo cáo ghép. Truy cập vào ngày 10 tháng 8 năm 2019, từ https://docs.python.org/3/reference/compound_stmts.html

Sweigart, A. (2015). Tự động hóa những thứ nhàm chán với Python: Lập trình thực tế cho toàn bộ người mới bắt đầu. San Francisco, CA: Không có báo chí tinh bột.

Xuất bản ngày 30 tháng 8 năm 2019.

«Tất cả Python nếu/bài viết khác

IF-ELES: Nếu điều kiện là đúng, thì khối mã bên trong câu lệnh IF được thực thi; Mặt khác, khối mã bên trong cái khác được thực thi. If-else-if: Nếu điều kiện là đúng, thì khối mã bên trong câu lệnh IF được thực thi.

Một tầng là gì nếuif (condition1) { // do one thing } if (condition2) { // do other thing } Here, if condition1 is true, one thing will be done. Again, if condition2 is true, other thing will be done too. Nested: if (condition1) { // do one thing if (condition2) { // do other thing } }

Sự khác biệt giữa nếu và nếu

Trong nếu, các câu lệnh bên trong khối if sẽ thực thi, nếu điều kiện là đúng và điều khiển được chuyển sang câu lệnh tiếp theo sau khối if.Trong IF khác, nếu điều kiện là đúng, các câu lệnh bên trong khối IF sẽ thực thi và nếu điều kiện là sai thì các câu lệnh trong khối IF khác sẽ thực thi.

Sự khác biệt giữa nếu

Không thực sự có một sự khác biệt.IF, nếu không, khác có điều kiện thực sự giống như cái lồng nhau với một trong các vỏ {} bị loại bỏ.Khi bạn chỉ có một câu lệnh duy nhất trong vỏ {}, bạn thường có thể loại bỏ {}.. The if, else if, else conditional is actually the same as the nested one with one of the {} enclosures removed. When you have only a single statement within a {} enclosure, you can usually eliminate the {}.

Sự khác biệt giữa nếu

IF-ELES: Nếu điều kiện là đúng, thì khối mã bên trong câu lệnh IF được thực thi;Mặt khác, khối mã bên trong cái khác được thực thi.If-else-if: Nếu điều kiện là đúng, thì khối mã bên trong câu lệnh IF được thực thi.