Các câu lệnh có điều kiện rất hữu ích cho việc ra quyết định và là một khái niệm cốt lõi trong tất cả các ngôn ngữ lập trình
Trong bài viết này, bạn sẽ học cách viết câu lệnh điều kiện trong Python
Cụ thể, bạn sẽ học cách viết các câu lệnh
language = input["Please enter your favorite programming language: "]
6, language = input["Please enter your favorite programming language: "]
7 và language = input["Please enter your favorite programming language: "]
8 [còn được gọi là language = input["Please enter your favorite programming language: "]
9] bằng PythonĐây là những gì chúng tôi sẽ đề cập
Câu lệnh language = input["Please enter your favorite programming language: "]
6 trong Python là gì?
language = input["Please enter your favorite programming language: "]
Câu lệnh
language = input["Please enter your favorite programming language: "]
6 còn được gọi là câu lệnh có điều kiện và câu lệnh có điều kiện là yếu tố chính của quá trình ra quyết địnhMột tuyên bố có điều kiện thực hiện một hành động cụ thể dựa trên kiểm tra hoặc so sánh
Nói chung, một tuyên bố
language = input["Please enter your favorite programming language: "]
6 đưa ra quyết định dựa trên một điều kiệnĐiều kiện là một biểu thức Boolean. Một biểu thức Boolean chỉ có thể là một trong hai giá trị –
# output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
0 hoặc # output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
1Vì vậy, về cơ bản, một tuyên bố
language = input["Please enter your favorite programming language: "]
6 nói. "Chỉ chạy đoạn mã sau một lần khi và chỉ khi điều kiện này ước tính là # output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
0. Nếu không, thì đừng chạy mã này. Chỉ cần bỏ qua nó và bỏ qua nó hoàn toàn "Cách tạo một câu lệnh language = input["Please enter your favorite programming language: "]
6 trong Python - Phân tích cú pháp
language = input["Please enter your favorite programming language: "]
Cú pháp chung cho một câu lệnh
language = input["Please enter your favorite programming language: "]
6 trong Python như sauif expression:
#run this code if expression evaluates to True
code statement[s]
Hãy phá vỡ nó
- Bạn bắt đầu câu lệnh
6 bằng cách sử dụng từ khóalanguage = input["Please enter your favorite programming language: "]
6language = input["Please enter your favorite programming language: "]
- Bạn để lại một khoảng trắng và sau đó thêm một giá trị Boolean. Một giá trị Boolean sẽ là một biểu thức có giá trị là
0 hoặc# output # Please enter your favorite programming language: Python # Correct! Of course it is Python!
1# output # Please enter your favorite programming language: Python # Correct! Of course it is Python!
- Sau đó, bạn thêm dấu hai chấm,
0#output # Please enter your favorite programming language: Java
- Trên một dòng mới, thêm một mức thụt đầu dòng. Nhiều trình soạn thảo mã sẽ tự động làm việc này cho bạn. Ví dụ: khi sử dụng trình chỉnh sửa Visual Studio Code với phần mở rộng Python, ngay sau khi bạn viết dấu hai chấm ở bước trước và nhấn
1, nó sẽ tự động thụt lề mã của bạn với mức độ thụt đầu dòng phù hợp. Mức độ thụt đầu dòng này là cách Python biết rằng các câu lệnh mã bạn sẽ viết được liên kết với câu lệnh#output # Please enter your favorite programming language: Java
6language = input["Please enter your favorite programming language: "]
- Cuối cùng, viết bất kỳ dòng mã nào. Những dòng này sẽ chạy khi và chỉ khi biểu thức ước tính là
0. Nếu biểu thức đánh giá là# output # Please enter your favorite programming language: Python # Correct! Of course it is Python!
1 thì chúng sẽ không chạy# output # Please enter your favorite programming language: Python # Correct! Of course it is Python!
Ví dụ về câu lệnh language = input["Please enter your favorite programming language: "]
6 trong Python là gì?
language = input["Please enter your favorite programming language: "]
Tiếp theo, hãy xem một ví dụ về tuyên bố
language = input["Please enter your favorite programming language: "]
6 đang hoạt độngTôi muốn nhắc người dùng nhập ngôn ngữ lập trình yêu thích của họ và lưu trữ câu trả lời của họ trong một biến có tên
#output
# Please enter your favorite programming language: Java
7language = input["Please enter your favorite programming language: "]
Sau đó, tôi sẽ đặt điều kiện
Nếu người dùng nhập ____18_______8 làm ngôn ngữ yêu thích của họ, thì và chỉ sau đó, tôi muốn in một thông báo tới bảng điều khiển nói rằng đây là câu trả lời đúng
Vì vậy, điều kiện sẽ kiểm tra xem giá trị được lưu trong biến
#output
# Please enter your favorite programming language: Java
7 có bằng với #output
# Please enter your favorite programming language: Java
8 hay khôngĐối với điều này, bạn sử dụng toán tử đẳng thức [
language = input["Please enter your favorite programming language: "]
if language == "Python":
# Don't do this!
print["Correct! Of course it is Python!"]
#output
# print["Correct! Of course it is Python!"]
# ^
# IndentationError: expected an indented block after 'if' statement on line 3
1 ] để kiểm tra xem giá trị được lưu trong biến #output
# Please enter your favorite programming language: Java
7 có bằng với chuỗi #output
# Please enter your favorite programming language: Java
8 khônglanguage = input["Please enter your favorite programming language: "]
if language == "Python":
print["Correct! Of course it is Python!"]
Tôi chạy mã của mình và khi lời nhắc “Vui lòng nhập ngôn ngữ lập trình yêu thích của bạn. ” xuất hiện, tôi nhập
#output
# Please enter your favorite programming language: Java
8sau đó tôi nhận được đầu ra sau
# output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
Điều kiện [
language = input["Please enter your favorite programming language: "]
if language == "Python":
# Don't do this!
print["Correct! Of course it is Python!"]
#output
# print["Correct! Of course it is Python!"]
# ^
# IndentationError: expected an indented block after 'if' statement on line 3
5] là # output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
0, vì vậy mã trong câu lệnh language = input["Please enter your favorite programming language: "]
6 thực thiNếu tôi chạy lại chương trình của mình và nhập một ngôn ngữ lập trình khác, sẽ không có đầu ra vì điều kiện sẽ là
# output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
1Mã bên trong câu lệnh
language = input["Please enter your favorite programming language: "]
6 sẽ không chạy và câu lệnh language = input["Please enter your favorite programming language: "]
6 sẽ bị bỏ qua hoàn toàn#output
# Please enter your favorite programming language: Java
Tại thời điểm này, điều đáng nói là bạn nên đảm bảo thụt lề mã bên trong câu lệnh
language = input["Please enter your favorite programming language: "]
6. Nếu bạn quên thụt lề câu lệnh in đó, cuối cùng bạn sẽ nhận được lỗi thụt lề saulanguage = input["Please enter your favorite programming language: "]
if language == "Python":
# Don't do this!
print["Correct! Of course it is Python!"]
#output
# print["Correct! Of course it is Python!"]
# ^
# IndentationError: expected an indented block after 'if' statement on line 3
Câu lệnh language = input["Please enter your favorite programming language: "]
7 trong Python là gì?
language = input["Please enter your favorite programming language: "]
Tự mình viết
language = input["Please enter your favorite programming language: "]
6 câu, đặc biệt là nhiều câu, không hữu ích lắm. Nó cũng không được coi là phương pháp hay nhất khi chương trình ngày càng lớn hơn. Đây là lý do tại sao câu lệnh language = input["Please enter your favorite programming language: "]
6 thường đi kèm với câu lệnh if condition:
#run this code if condition is True
code statement[s]
else:
# if the condition above is False run this code
code statement[s]
5Tuyên bố
language = input["Please enter your favorite programming language: "]
7 về cơ bản nói. "language = input["Please enter your favorite programming language: "]
6 điều kiện này là Đúng, hãy làm điều sau đây, if condition:
#run this code if condition is True
code statement[s]
else:
# if the condition above is False run this code
code statement[s]
5 thay vào đó hãy làm điều này"Mã bên trong câu lệnh
if condition:
#run this code if condition is True
code statement[s]
else:
# if the condition above is False run this code
code statement[s]
5 là mã bạn muốn chạy khi và chỉ khi điều kiện bạn đặt trong câu lệnh language = input["Please enter your favorite programming language: "]
6 của mình có giá trị là # output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
1Nếu điều kiện trong câu lệnh
language = input["Please enter your favorite programming language: "]
6 của bạn ước tính thành # output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
0 thì mã bên trong câu lệnh if condition:
#run this code if condition is True
code statement[s]
else:
# if the condition above is False run this code
code statement[s]
5 sẽ không bao giờ chạyTừ khóa
if condition:
#run this code if condition is True
code statement[s]
else:
# if the condition above is False run this code
code statement[s]
5 là giải pháp khi điều kiện language = input["Please enter your favorite programming language: "]
6 là Sai và mã bên trong khối language = input["Please enter your favorite programming language: "]
6 không chạy. Nó cung cấp một giải pháp thay thếCú pháp chung cho một câu lệnh
language = input["Please enter your favorite programming language: "]
7 trong Python như sauif condition:
#run this code if condition is True
code statement[s]
else:
# if the condition above is False run this code
code statement[s]
Ví dụ về câu lệnh language = input["Please enter your favorite programming language: "]
7 trong Python là gì?
language = input["Please enter your favorite programming language: "]
Hãy xem lại ví dụ từ trước đó
language = input["Please enter your favorite programming language: "]
if language == "Python":
print["Correct! Of course it is Python!"]
Như bạn đã thấy trước đây, khi tôi nhập chuỗi
#output
# Please enter your favorite programming language: Java
8, mã trong hàm language = input["Please enter your favorite programming language: "]
if language == "Python":
print["Correct! Of course it is Python!"]
else:
print["Hmm..Are you sure that it is not Python??"]
1 sẽ chạy vì điều kiện ước tính là # output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
0Tuy nhiên, không có cách nào khác khi người dùng nhập nội dung nào đó không bằng chuỗi
#output
# Please enter your favorite programming language: Java
8Đây là lúc câu lệnh
if condition:
#run this code if condition is True
code statement[s]
else:
# if the condition above is False run this code
code statement[s]
5 trở nên hữu ích và được thêm vào câu lệnh language = input["Please enter your favorite programming language: "]
6language = input["Please enter your favorite programming language: "]
if language == "Python":
print["Correct! Of course it is Python!"]
else:
print["Hmm..Are you sure that it is not Python??"]
Nếu điều kiện là
# output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
1, mã trong câu lệnh language = input["Please enter your favorite programming language: "]
6 sẽ bị bỏ qua và bỏ qua. Thay vào đó, mã trong câu lệnh if condition:
#run this code if condition is True
code statement[s]
else:
# if the condition above is False run this code
code statement[s]
5 chạy# output
# Please enter your favorite programming language: Java
# Hmm..Are you sure that it is not Python??
Một điều cần lưu ý tại thời điểm này là bạn không thể viết thêm bất kỳ mã nào ở giữa câu lệnh
language = input["Please enter your favorite programming language: "]
7language = input["Please enter your favorite programming language: "]
0Câu lệnh language = input["Please enter your favorite programming language: "]
8 trong Python là gì?
language = input["Please enter your favorite programming language: "]
language = input["Please enter your favorite programming language: "]
8 có nghĩa là language = input["Please enter your favorite programming language: "]
9Khi bạn muốn đặt thêm điều kiện và không chỉ có câu lệnh
language = input["Please enter your favorite programming language: "]
6 và if condition:
#run this code if condition is True
code statement[s]
else:
# if the condition above is False run this code
code statement[s]
5 để lựa chọn, bạn có thể đưa ra câu lệnh language = input["Please enter your favorite programming language: "]
8Nếu câu lệnh
language = input["Please enter your favorite programming language: "]
6 là # output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
1 Python sẽ chuyển sang câu lệnh language = input["Please enter your favorite programming language: "]
8 và cố gắng kiểm tra điều kiện được đặt trong khối đóBạn cũng có thể viết nhiều khối
language = input["Please enter your favorite programming language: "]
8, tùy thuộc vào nhiều tùy chọn mà bạn muốn cóMột tuyên bố
language = input["Please enter your favorite programming language: "]
8 về cơ bản có nghĩa là. "Nếu điều kiện này là True, hãy làm như sau. Nếu không, hãy thử làm điều này để thay thế. Tuy nhiên, nếu không có điều nào ở trên là Đúng và tất cả những điều khác đều thất bại, cuối cùng hãy làm điều này. "Cú pháp chung cho một câu lệnh
language = input["Please enter your favorite programming language: "]
8 như saulanguage = input["Please enter your favorite programming language: "]
1Mã được đánh giá theo thứ tự nó được viết, từ trên xuống dưới
Khi Python tìm thấy một điều kiện có giá trị là
# output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
0, nó sẽ chạy mã trong khối đó và bỏ qua phần còn lạiVì vậy, nếu mã trong khối
language = input["Please enter your favorite programming language: "]
6 là # output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
0, thì không có khối nào khác sẽ chạy. Chúng sẽ bị bỏ qua và bỏ quaNếu mã trong khối
language = input["Please enter your favorite programming language: "]
6 là # output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
1, nó sẽ chuyển sang khối language = input["Please enter your favorite programming language: "]
8Nếu đó là
# output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
0, thì các khối còn lại sẽ bị bỏ quaNếu là
# output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
1, Python sẽ chuyển sang các khối language = input["Please enter your favorite programming language: "]
8 khác nếu có bất kỳ khối nàoCuối cùng, nếu tất cả các điều kiện là
# output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
1, thì và chỉ sau đó mã trong khối if condition:
#run this code if condition is True
code statement[s]
else:
# if the condition above is False run this code
code statement[s]
5 sẽ chạy. Khối if condition:
#run this code if condition is True
code statement[s]
else:
# if the condition above is False run this code
code statement[s]
5 về cơ bản có nghĩa là "khi vẫn thất bại, hãy chạy mã này thay thế"Ví dụ về câu lệnh language = input["Please enter your favorite programming language: "]
8 trong Python là gì?
language = input["Please enter your favorite programming language: "]
Hãy xem một ví dụ về cách hoạt động của câu lệnh
language = input["Please enter your favorite programming language: "]
8Hãy lấy ví dụ sau
language = input["Please enter your favorite programming language: "]
2Nếu câu lệnh
language = input["Please enter your favorite programming language: "]
6 là # output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
0, phần còn lại của mã sẽ bị bỏ qualanguage = input["Please enter your favorite programming language: "]
3Khi câu lệnh
language = input["Please enter your favorite programming language: "]
6 là # output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
1, Python chuyển sang khối language = input["Please enter your favorite programming language: "]
8 và kiểm tra điều kiện đó. Nếu câu lệnh language = input["Please enter your favorite programming language: "]
8 là # output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
0, phần còn lại của mã sẽ bị bỏ quaNếu là
# output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
0, Python sẽ chạy mã trong khối language = input["Please enter your favorite programming language: "]
8 và bỏ qua phần còn lại của mãlanguage = input["Please enter your favorite programming language: "]
4Nếu cả hai điều kiện trước đều là
# output
# Please enter your favorite programming language: Python
# Correct! Of course it is Python!
1, thì biện pháp cuối cùng là khối if condition:
#run this code if condition is True
code statement[s]
else:
# if the condition above is False run this code
code statement[s]
5language = input["Please enter your favorite programming language: "]
5Phần kết luận
Và bạn có nó rồi đấy. Bây giờ bạn đã biết cách viết các câu lệnh
language = input["Please enter your favorite programming language: "]
6, language = input["Please enter your favorite programming language: "]
7 và language = input["Please enter your favorite programming language: "]
8 bằng PythonTôi hy vọng bạn thấy hướng dẫn này hữu ích
Để tìm hiểu thêm về ngôn ngữ lập trình Python, hãy xem chứng chỉ Python của freeCodeCamp
Mã hóa vui vẻ
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Học một cái gì đó mới mỗi ngày và viết về nó
Nếu bài viết này hữu ích, hãy tweet nó
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu