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
8 theo sau là thử nghiệm có điều kiện đầu tiên.How many words did you write? 10 A quick comment; helpful too!
- Một hoặc nhiều từ khóa
5 kiểm tra các điều kiện bổ sung.How many words did you write? 10 A quick comment; helpful too!
- Và một điều khoản
7 tùy chọn chạy khi tất cả các điều kiện làHow many words did you write? 10 A quick comment; helpful too!
9.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!"]
Đặ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
7, mỗi khối mã phải vượt qua thử nghiệmHow many words did you write? 10 A quick comment; helpful too!
8 hoặcHow 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àHow many words did you write? 10 A quick comment; helpful too!
9, không có gì của câu lệnh được thực hiệ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!"]
- Với
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àHow many words did you write? 10 A quick comment; helpful too!
9.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!"]
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
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.How many words did you write? 10 A quick comment; helpful too!
- Mỗi dòng
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 [How many words did you write? 10 A quick comment; helpful too!
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 766 and guess 675 and guess 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
3, thì chúng ta cũng có thể suy luận rằng các từ dưới 35.000. Vì vậy,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 766 and guess 675 and guess 1000: print["Way too high! Guess lower."] elif guess 766 and guess 675 and guess 1000: print["Way too high! Guess lower."] elif guess 766 and guess 675 and guess 1000: print["Way too high! Guess lower."] elif guess 766 and guess 675 and guess 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 xem xét rằng một bài nghiên cứu.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!
- Thử nghiệm thứ tư nhìn thấy nếu số từ là trên 2k [
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 đề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."]
5 tiếp theo.How many words did you write? 10 A quick comment; helpful too!
- So sánh thứ năm có vẻ nếu có hơn 500 từ [
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.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."]
- Các thử nghiệm mệnh đề
5 cuối cùngHow many words did you write? 10 A quick comment; helpful too!
4. Khi đây 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."]
3, hàmwordsWritten = 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 xem xét phạm vi 201-500 từ một email.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!
- Điều khoản
7 chạy khi không có điều kiện nào trước đây làHow many words did you write? 10 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.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!"]
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 766 and guess 675 and guess 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!"]
0Trong 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
3, làm cho chương trình thực thi một số mã.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!"]
- 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ã
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.How many words did you write? 10 A quick comment; helpful too!
- 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ừ //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