Biến bên trong câu lệnh if python

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ì?

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 định

Mộ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!
1

Vì 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

Cú pháp chung cho một câu lệnh

language = input("Please enter your favorite programming language: ")
6 trong Python như sau

if 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
    language = input("Please enter your favorite programming language: ")
    
    6 bằng cách sử dụng từ khóa
    language = input("Please enter your favorite programming language: ")
    
    6
  • 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à
    # 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!
    
    1
  • Sau đó, bạn thêm dấu hai chấm,
    #output 
    
    # Please enter your favorite programming language: Java
    
    0
  • 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
    #output 
    
    # Please enter your favorite programming language: Java
    
    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
    language = input("Please enter your favorite programming language: ")
    
    6
  • 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à
    # output
    
    # Please enter your favorite programming language: Python
    # Correct! Of course it is Python!
    
    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

Ví dụ về câu lệnh language = input("Please enter your favorite programming language: ") 6 trong Python là gì?

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 động

Tô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
7

language = 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ông

language = 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
8

sau đó 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 thi

Nế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!
1

Mã 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ề sau

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

Câu lệnh language = input("Please enter your favorite programming language: ") 7 trong Python là gì?

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)
5

Tuyê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!
1

Nế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ạy

Từ 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ư sau

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)

Ví dụ về câu lệnh language = input("Please enter your favorite programming language: ") 7 trong Python là gì?

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!
0

Tuy 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: ")
6

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??")

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: ")
7

language = input("Please enter your favorite programming language: ")
0

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: ")
8 có nghĩa là
language = input("Please enter your favorite programming language: ")
9

Khi 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: ")
8

Nế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ư sau

language = input("Please enter your favorite programming language: ")
1

Mã đượ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ại

Vì 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ỏ qua

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!
1, nó sẽ chuyển sang khối
language = input("Please enter your favorite programming language: ")
8

Nế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ỏ qua

Nế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ào

Cuố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ì?

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: ")
8

Hãy lấy ví dụ sau

language = input("Please enter your favorite programming language: ")
2

Nế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ỏ qua

language = input("Please enter your favorite programming language: ")
3

Khi 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ỏ qua

Nế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: ")
4

Nế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)
5

language = input("Please enter your favorite programming language: ")
5

Phầ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 Python

Tô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


Biến bên trong câu lệnh if python
Dionysia Lemonaki

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

Bạn có thể đặt một biến trong câu lệnh if trong Python không?

Các biến Python nằm trong phạm vi hàm, lớp hoặc mô-đun trong cùng mà chúng được gán. Các khối điều khiển như khối if và khối while không được tính, vì vậy một biến được gán bên trong if vẫn thuộc phạm vi của một hàm, lớp hoặc mô-đun .

Bạn có thể khai báo biến trong câu lệnh IF không?

Phạm vi biến. Java cho phép bạn khai báo các biến trong phần thân của câu lệnh while hoặc if , nhưng điều quan trọng cần nhớ là những điều sau. Một biến chỉ có sẵn từ phần khai báo của nó cho đến hết dấu ngoặc nhọn mà nó được khai báo.

Bạn có thể truy cập một biến bên trong hàm Python không?

Trong Python và hầu hết các ngôn ngữ lập trình, các biến được khai báo bên ngoài hàm được gọi là biến toàn cục. Bạn có thể truy cập các biến như vậy bên trong và bên ngoài hàm, vì chúng có phạm vi toàn cục .

phạm vi của biến bên trong nếu là gì?

Phạm vi của một biến trong câu lệnh If . Các biến không được kiểm tra tại thời điểm biên dịch, đó là lý do tại sao các ngôn ngữ khác đưa ra một ngoại lệ. Trong Python, miễn là biến tồn tại vào thời điểm bạn yêu cầu, không có ngoại lệ nào được ném ra. anything declared in an if block has the same scope as anything declared outside the block. Variables are not checked at compile-time, which is why other languages throw an exception. In Python, so long as the variable exists at the time you require it, no exception is thrown.