Xem thảo luận Show 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
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. 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:
[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
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
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 Có thể có 2 nếu các câu trong Python?Chúng ta có thể có một nếu ... Elif ... khác tuyên bố bên trong người khác nếu ... Elif ... khác tuyên bố.Điều này được gọi là làm tổ trong lập trình máy tính.Bất kỳ số lượng câu này có thể được lồng bên trong nhau.Any number of these statements can be nested inside one another.
Nếu tuyên bố có 2 điều kiện?Hàm IF cho phép bạn thực hiện so sánh hợp lý giữa một giá trị và những gì bạn mong đợi bằng cách kiểm tra điều kiện và trả về kết quả nếu đúng hoặc sai.Vì vậy, một tuyên bố nếu có thể có hai kết quả.an IF statement can have two results. |