Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Syntax:
if [condition]: code1 else: code2
[on_true] if [expression] else [on_false]
Bàn luận For more information, refer to Decision Making in Python [if , if..else, Nested if, if-elif]
Tuyên bố có điều kiện nếu thay đổi được sử dụng trong Python khi một tình huống dẫn đến hai điều kiện và một trong số chúng sẽ đúng.
Lưu ý: Để biết thêm thông tin, hãy tham khảo ra quyết định trong Python [nếu, nếu..else, lồng nhau nếu, if-elif]
Syntax:
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code2
- Nhiều điều kiện trong nếu tuyên bố = for this to work normally both conditions provided with should be true. If the first condition falls false, the compiler doesn’t check the second one. If the first condition is true and the compiler moves to the second and if the second comes out to be false, false is returned to the if statement.
- Ở đây chúng tôi sẽ nghiên cứu làm thế nào chúng tôi có thể kiểm tra nhiều điều kiện trong một câu lệnh IF. Điều này có thể được thực hiện bằng cách sử dụng ‘và hoặc hoặc‘ hoặc hoặc cả hai trong một tuyên bố. = for this to work normally either condition needs to be true. The compiler checks the first condition first and if that turns out to be true, the compiler runs the assigned code and the second condition is not evaluated. If the first condition turns out to be false, the compiler checks the second, if that is true the assigned code runs but if that fails too, false is returned to the if statement.
và so sánh = để điều này hoạt động bình thường cả hai điều kiện được cung cấp là đúng. Nếu điều kiện đầu tiên rơi vào sai, trình biên dịch không kiểm tra cái thứ hai. Nếu điều kiện đầu tiên là đúng và trình biên dịch chuyển sang lần thứ hai và nếu lần thứ hai xuất hiện là sai, sai sẽ được trả lại cho câu lệnh IF.
PROGRAM 1: program that grants access only to kids aged between 8-12
hoặc so sánh = để điều này hoạt động bình thường một trong hai điều kiện cần phải đúng. Trình biên dịch kiểm tra điều kiện đầu tiên trước tiên và nếu điều đó hóa ra là đúng, trình biên dịch chạy mã được gán và điều kiện thứ hai không được đánh giá. Nếu điều kiện đầu tiên hóa ra là sai, trình biên dịch sẽ kiểm tra lần thứ hai, nếu đó là đúng, mã được chỉ định chạy nhưng nếu điều đó cũng không thành công, sai sẽ được trả lại cho câu lệnh IF.
Các ví dụ sau sẽ giúp hiểu điều này tốt hơn: Chương trình 1: Chương trình cấp quyền truy cập chỉ cho trẻ em trong độ tuổi từ 8-12
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code23
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code24
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code25
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code26
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code27
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code23
SORRY ! YOU ARE NOT ALLOWED. BYE !1
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code25
Output:
SORRY ! YOU ARE NOT ALLOWED. BYE !
PROGRAM 2:
age
=
[on_true] if [expression] else [on_false]0
[on_true] if [expression] else [on_false]1
[on_true] if [expression] else [on_false]2
=
[on_true] if [expression] else [on_false]4
[on_true] if [expression] else [on_false]5
[on_true] if [expression] else [on_false]6
[on_true] if [expression] else [on_false]7
=
chương trình kiểm tra thỏa thuận của người dùng theo các điều khoản
SORRY ! YOU ARE NOT ALLOWED. BYE !3
=
SORRY ! YOU ARE NOT ALLOWED. BYE !5
[on_true] if [expression] else [on_false]1
SORRY ! YOU ARE NOT ALLOWED. BYE !7
=
=
PROGRAM 2:0
PROGRAM 2:1
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code23
9 is the largest6
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code25
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code26
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code27
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code23
working3
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code25
Output:
YOU SAID NO
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]:
code1
else:
code2
1if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]:
code1
else:
code2
223YOU SAID NO
05025 program to compare the entered three numbers
Các
Chương trình 3: Chương trình so sánh ba số đã nhập
working5
=
working7
working8
=
age
0if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
=
8=
9if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code25
age
1=
age
3
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
[on_true] if [expression] else [on_false]15
=
9if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code25
[on_true] if [expression] else [on_false]1
age
5[on_true] if [expression] else [on_false]6
age
7[on_true] if [expression] else [on_false]6
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
[on_true] if [expression] else [on_false]32
=
9if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code25
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code26
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code27
YOU SAID NO2
[on_true] if [expression] else [on_false]022
Output:
9 is the largest
YOU SAID NO2
[on_true] if [expression] else [on_false]19
[on_true] if [expression] else [on_false]6
[on_true] if [expression] else [on_false]21
[on_true] if [expression] else [on_false]6
[on_true] if [expression] else [on_false]23__
PROGRAM 4:
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code23
[on_true] if [expression] else [on_false]40
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code25
Không chỉ hai điều kiện chúng ta có thể kiểm tra nhiều hơn thế bằng cách sử dụng ‘và và và‘ or.
working5
=
[on_true] if [expression] else [on_false]44
working8
=
[on_true] if [expression] else [on_false]44
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code23
[on_true] if [expression] else [on_false]70
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code25
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code26
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code27
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code23
[on_true] if [expression] else [on_false]77
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code25
Output:
working