Hướng dẫn nested if else in python exercises - lồng nhau if else trong bài tập python

Tại sao mã đó không hoạt động:

def minimal_three(a,b,c):
    if a < b:
        if a < c:
            return (a)
        else:
            # what if a >= c and a < b ?
            return "i returned nothing"
    elif b < a:
        if b < c:
            return (b)
        else:
            # what if b >= c and a > b ?
            return "i returned nothing"
    elif c < a:
        if c < b:
            return (c)
        else:
            # what if b <= c and a < c ?
            return "i returned nothing"
    else:
        return 'none'

Alternative:

def min_of_two(a, b):
    if a > b:
        return b
    return a

def min_of_three(a, b, c):
    min_ab = min_of_two(a, b)
    min_abc = min_of_two(min_ab, c)
    return min_abc

def min_of_three_v2(a, b, c):
    min_ab = a
    if a > b:
        min_ab = b
    min_abc = min_ab
    if min_ab > c:
        min_abc = c
    return min_abc

def min_of_three_v3(a, b, c):
    min_abc = a
    if min_abc > b:
        min_abc = b
    if min_abc > c:
        min_abc = c
    return min_abc

Nếu bạn thực sự muốn sử dụng lồng nhau nếu/khác (mã này quá dài):

# if-elif-else is ok.
# nested if is hard to read
# if-elif-elif-elif-elif...-else is hard to read.
# hard to read == easy to have bugs, which is bad.

def min_abc_slower(a, b, c):
    if a > b:
        # a > b. This means min(a, b) == b
        if b > c:
            # b > c. This means min(c, min(a, b)) == c
            return c
        else:
            # b > c is False. This means b <= c.
            # So, min(c, min(a, b)) == b
            return b
    else:
        # a > b is False. This means a <= b.
        # So, min(a, b) = a
        if a > c:
            # a > c. This means min(c, min(a, b)) == c
            return c
        else:
            # a > c is False. This means a <= c
            # So, min(c, min(a, b)) == a
            return a

Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:50:47 (UTC/GMT +8 giờ)

Các câu lệnh và vòng lặp có điều kiện Python [44 bài tập với giải pháp]

[Một trình soạn thảo có sẵn ở cuối trang để viết và thực thi các tập lệnh.]

1. Viết một chương trình Python để tìm những con số chia hết cho 7 và bội số 5, từ 1500 đến 2700 (cả hai bao gồm). Chuyển đến biên tập viên Nhấp vào tôi để xem giải pháp mẫu Write a Python program to find those numbers which are divisible by 7 and multiple of 5, between 1500 and 2700 (both included). Go to the editor
Click me to see the sample solution

2. Viết một chương trình Python để chuyển đổi nhiệt độ thành và từ Celsius, Fahrenheit. Chuyển đến Trình chỉnh sửa [Công thức: C/5 = F-32/9 [trong đó C = Nhiệt độ trong Celsius và F = Nhiệt độ trong Fahrenheit] Đầu ra dự kiến: 60 ° C là 140 trong Fahrenheit 45 ° F là 7 ở Celsius, nhấp vào tôi để tôi Xem giải pháp mẫuWrite a Python program to convert temperatures to and from celsius, fahrenheit. Go to the editor
[ Formula : c/5 = f-32/9 [ where c = temperature in celsius and f = temperature in fahrenheit ]
Expected Output :
60°C is 140 in Fahrenheit
45°F is 7 in Celsius
Click me to see the sample solution

3. Viết một chương trình Python để đoán một số từ 1 đến 9. Truy cập ghi chú của trình soạn thảo: Người dùng được nhắc nhập phỏng đoán. Nếu người dùng đoán sai thì lời nhắc sẽ xuất hiện trở lại cho đến khi đoán là chính xác, theo đoán thành công, người dùng sẽ nhận được "đoán tốt!" Tin nhắn, và chương trình sẽ thoát. Nhấp vào tôi để xem giải pháp mẫuWrite a Python program to guess a number between 1 to 9. Go to the editor
Note : User is prompted to enter a guess. If the user guesses wrong then the prompt appears again until the guess is correct, on successful guess, user will get a "Well guessed!" message, and the program will exit.
Click me to see the sample solution

4. Viết một chương trình Python để xây dựng mẫu sau, sử dụng một vòng lặp được lồng cho vòng lặp.Write a Python program to construct the following pattern, using a nested for loop.

* 
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

Nhấp vào tôi để xem giải pháp mẫu

5. Viết một chương trình Python chấp nhận một từ từ người dùng và đảo ngược nó. Chuyển đến biên tập viên Nhấp vào tôi để xem giải pháp mẫu Write a Python program that accepts a word from the user and reverse it. Go to the editor
Click me to see the sample solution

6. Viết một chương trình Python để đếm số lượng số chẵn và số lẻ từ một loạt các số. Chuyển đến các số mẫu biên tập: Số & nbsp; = & nbsp; (1, & nbsp; 2, & nbsp; 3, & nbsp; 4, & nbsp; 5, & nbsp; 6, & nbsp; 7, & nbsp; 8, & nbsp; 9) Đầu ra dự kiến: Số số chẵn: 5 số số lẻ: 4 Nhấp vào tôi để xem giải pháp mẫu Write a Python program to count the number of even and odd numbers from a series of numbers. Go to the editor
Sample numbers : numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9) 
Expected Output :
Number of even numbers : 5
Number of odd numbers : 4
Click me to see the sample solution

7. Viết một chương trình Python in từng mục và loại tương ứng từ danh sách sau. Danh sách mẫu: Datalist = [1452, 11,23, 1+2J, true, 'w3Resource', (0, -1), [5, 12], {"class": 'v', "Phần": 'a'} ] Nhấp vào tôi để xem giải pháp mẫu Write a Python program that prints each item and its corresponding type from the following list.
Sample List : datalist = [1452, 11.23, 1+2j, True, 'w3resource', (0, -1), [5, 12], {"class":'V', "section":'A'}]
Click me to see the sample solution

8. Viết chương trình Python in tất cả các số từ 0 đến 6 ngoại trừ 3 và 6. Lưu ý: Sử dụng câu lệnh 'Tiếp tục'. Đầu ra dự kiến: 0 1 2 4 5 Nhấp vào tôi để xem giải pháp mẫu Write a Python program that prints all the numbers from 0 to 6 except 3 and 6.
Note : Use 'continue' statement.
Expected Output : 0 1 2 4 5
Click me to see the sample solution

9. ... Mỗi số tiếp theo được tìm thấy bằng cách thêm hai số trước nó. Đầu ra dự kiến: 1 1 2 3 5 8 13 21 34 Nhấp vào tôi để xem giải pháp mẫu Write a Python program to get the Fibonacci series between 0 to 50. Go to the editor
Note : The Fibonacci Sequence is the series of numbers :
0, 1, 1, 2, 3, 5, 8, 13, 21, ....
Every next number is found by adding up the two numbers before it.
Expected Output : 1 1 2 3 5 8 13 21 34
Click me to see the sample solution

10. Viết một chương trình Python lặp lại các số nguyên từ 1 đến 50. Đối với bội số của ba bản in "fizz" thay vì số và cho bội số của năm in "buzz". Đối với các số là bội số của cả ba và năm in "fizzbuzz". Đầu ra mẫu: FizzBuzz 1 2 Fizz 4 Buzz Nhấp vào tôi để xem giải pháp mẫu Write a Python program which iterates the integers from 1 to 50. For multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
Sample Output :
fizzbuzz
1
2
fizz
4
buzz
Click me to see the sample solution

11. Viết một chương trình Python mất hai chữ số m (hàng) và n (cột) làm đầu vào và tạo ra một mảng hai chiều. Giá trị phần tử trong hàng thứ i và cột thứ J của mảng phải là i*j. Chuyển đến trình soạn thảo Lưu ý: i = 0,1 .., M-1 J = 0,1, N-1. Write a Python program which takes two digits m (row) and n (column) as input and generates a two-dimensional array. The element value in the i-th row and j-th column of the array should be i*j. Go to the editor
Note :
i = 0,1.., m-1
j = 0,1, n-1.

Dữ liệu kiểm tra: hàng = 3, cột = 4 Kết quả dự kiến: [[0, 0, 0, 0], [0, 1, 2, 3], [0, 2, 4, 6]] Nhấp vào tôi để xem mẫu dung dịch
Expected Result : [[0, 0, 0, 0], [0, 1, 2, 3], [0, 2, 4, 6]]
Click me to see the sample solution

12. Viết một chương trình Python chấp nhận một chuỗi các dòng (dòng trống để chấm dứt) làm đầu vào và in các dòng dưới dạng đầu ra (tất cả các ký tự trong chữ thường). Chuyển đến biên tập viên Nhấp vào tôi để xem giải pháp mẫuWrite a Python program that accepts a sequence of lines (blank line to terminate) as input and prints the lines as output (all characters in lower case). Go to the editor
Click me to see the sample solution

13. Truy cập dữ liệu mẫu biên tập: 0100.0011.1010.1001.1100.1001 Đầu ra dự kiến: 1010 Nhấp vào tôi để xem giải pháp mẫuWrite a Python program which accepts a sequence of comma separated 4 digit binary numbers as its input and print the numbers that are divisible by 5 in a comma separated sequence. Go to the editor
Sample Data : 0100,0011,1010,1001,1100,1001
Expected Output : 1010
Click me to see the sample solution

15. Viết chương trình Python để kiểm tra tính hợp lệ của đầu vào mật khẩu của người dùng. Chuyển đến xác thực biên tập:Write a Python program to check the validity of password input by users. Go to the editor
Validation :

  • Ít nhất 1 chữ cái giữa [A-Z] và 1 chữ cái giữa [A-Z].
  • Ít nhất 1 số giữa [0-9].
  • Ít nhất 1 ký tự từ [$#@].
  • Độ dài tối thiểu 6 ký tự.
  • Độ dài tối đa 16 ký tự.

Nhấp vào tôi để xem giải pháp mẫu

16. Viết một chương trình Python để tìm các số từ 100 đến 400 (cả hai bao gồm) trong đó mỗi chữ số của một số là một số chẵn. Các số thu được phải được in theo trình tự được phân tách bằng dấu phẩy. Chuyển đến biên tập viên Nhấp vào tôi để xem giải pháp mẫuWrite a Python program to find numbers between 100 and 400 (both included) where each digit of a number is an even number. The numbers obtained should be printed in a comma-separated sequence. Go to the editor
Click me to see the sample solution

17. Viết một chương trình Python để in mẫu bảng chữ cái 'A'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:Write a Python program to print alphabet pattern 'A'. Go to the editor
Expected Output:

  ***                                                                   
 *   *                                                                  
 *   *                                                                  
 *****                                                                  
 *   *                                                                  
 *   *                                                                  
 *   *

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:Write a Python program to print alphabet pattern 'D'. Go to the editor
Expected Output:

 ****                                                                   
 *   *                                                                  
 *   *                                                                  
 *   *                                                                  
 *   *                                                                  
 *   *                                                                  
 **** 

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:Write a Python program to print alphabet pattern 'E'. Go to the editor
Expected Output:

 *****                                                                  
 *                                                                      
 *                                                                      
 ****                                                                   
 *                                                                      
 *                                                                      
 *****

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:Write a Python program to print alphabet pattern 'G'. Go to the editor
Expected Output:

  ***                                                                   
 *   *                                                                  
 *                                                                      
 * ***                                                                  
 *   *                                                                  
 *   *                                                                  
  *** 

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:Write a Python program to print alphabet pattern 'L'. Go to the editor
Expected Output:

 *                                                                      
 *                                                                      
 *                                                                      
 *                                                                      
 *                                                                      
 *                                                                      
 *****

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:Write a Python program to print alphabet pattern 'M'. Go to the editor
Expected Output:

  *       *                                                             
  *       *                                                             
  * *   * *                                                             
  *   *   *                                                             
  *       *                                                             
  *       *                                                             
  *       *

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:Write a Python program to print alphabet pattern 'O'. Go to the editor
Expected Output:

def min_of_two(a, b):
    if a > b:
        return b
    return a

def min_of_three(a, b, c):
    min_ab = min_of_two(a, b)
    min_abc = min_of_two(min_ab, c)
    return min_abc

def min_of_three_v2(a, b, c):
    min_ab = a
    if a > b:
        min_ab = b
    min_abc = min_ab
    if min_ab > c:
        min_abc = c
    return min_abc

def min_of_three_v3(a, b, c):
    min_abc = a
    if min_abc > b:
        min_abc = b
    if min_abc > c:
        min_abc = c
    return min_abc
0

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:Write a Python program to print alphabet pattern 'P'. Go to the editor
Expected Output:

def min_of_two(a, b):
    if a > b:
        return b
    return a

def min_of_three(a, b, c):
    min_ab = min_of_two(a, b)
    min_abc = min_of_two(min_ab, c)
    return min_abc

def min_of_three_v2(a, b, c):
    min_ab = a
    if a > b:
        min_ab = b
    min_abc = min_ab
    if min_ab > c:
        min_abc = c
    return min_abc

def min_of_three_v3(a, b, c):
    min_abc = a
    if min_abc > b:
        min_abc = b
    if min_abc > c:
        min_abc = c
    return min_abc
1

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:Write a Python program to print alphabet pattern 'R'. Go to the editor
Expected Output:

def min_of_two(a, b):
    if a > b:
        return b
    return a

def min_of_three(a, b, c):
    min_ab = min_of_two(a, b)
    min_abc = min_of_two(min_ab, c)
    return min_abc

def min_of_three_v2(a, b, c):
    min_ab = a
    if a > b:
        min_ab = b
    min_abc = min_ab
    if min_ab > c:
        min_abc = c
    return min_abc

def min_of_three_v3(a, b, c):
    min_abc = a
    if min_abc > b:
        min_abc = b
    if min_abc > c:
        min_abc = c
    return min_abc
2

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:Write a Python program to print the following patterns. Go to the editor
Expected Output:

def min_of_two(a, b):
    if a > b:
        return b
    return a

def min_of_three(a, b, c):
    min_ab = min_of_two(a, b)
    min_abc = min_of_two(min_ab, c)
    return min_abc

def min_of_three_v2(a, b, c):
    min_ab = a
    if a > b:
        min_ab = b
    min_abc = min_ab
    if min_ab > c:
        min_abc = c
    return min_abc

def min_of_three_v3(a, b, c):
    min_abc = a
    if min_abc > b:
        min_abc = b
    if min_abc > c:
        min_abc = c
    return min_abc
3

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:
Expected Output:
def min_of_two(a, b):
    if a > b:
        return b
    return a

def min_of_three(a, b, c):
    min_ab = min_of_two(a, b)
    min_abc = min_of_two(min_ab, c)
    return min_abc

def min_of_three_v2(a, b, c):
    min_ab = a
    if a > b:
        min_ab = b
    min_abc = min_ab
    if min_ab > c:
        min_abc = c
    return min_abc

def min_of_three_v3(a, b, c):
    min_abc = a
    if min_abc > b:
        min_abc = b
    if min_abc > c:
        min_abc = c
    return min_abc
4

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:Write a Python program to print alphabet pattern 'U'. Go to the editor
Expected Output:

def min_of_two(a, b):
    if a > b:
        return b
    return a

def min_of_three(a, b, c):
    min_ab = min_of_two(a, b)
    min_abc = min_of_two(min_ab, c)
    return min_abc

def min_of_three_v2(a, b, c):
    min_ab = a
    if a > b:
        min_ab = b
    min_abc = min_ab
    if min_ab > c:
        min_abc = c
    return min_abc

def min_of_three_v3(a, b, c):
    min_abc = a
    if min_abc > b:
        min_abc = b
    if min_abc > c:
        min_abc = c
    return min_abc
5

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:Write a Python program to print alphabet pattern 'X'. Go to the editor
Expected Output:

def min_of_two(a, b):
    if a > b:
        return b
    return a

def min_of_three(a, b, c):
    min_ab = min_of_two(a, b)
    min_abc = min_of_two(min_ab, c)
    return min_abc

def min_of_three_v2(a, b, c):
    min_ab = a
    if a > b:
        min_ab = b
    min_abc = min_ab
    if min_ab > c:
        min_abc = c
    return min_abc

def min_of_three_v3(a, b, c):
    min_abc = a
    if min_abc > b:
        min_abc = b
    if min_abc > c:
        min_abc = c
    return min_abc
6

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:Write a Python program to print alphabet pattern 'Z'. Go to the editor
Expected Output:

def min_of_two(a, b):
    if a > b:
        return b
    return a

def min_of_three(a, b, c):
    min_ab = min_of_two(a, b)
    min_abc = min_of_two(min_ab, c)
    return min_abc

def min_of_three_v2(a, b, c):
    min_ab = a
    if a > b:
        min_ab = b
    min_abc = min_ab
    if min_ab > c:
        min_abc = c
    return min_abc

def min_of_three_v3(a, b, c):
    min_abc = a
    if min_abc > b:
        min_abc = b
    if min_abc > c:
        min_abc = c
    return min_abc
7

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên: Write a Python program to calculate a dog's age in dog's years. Go to the editor
Note: For the first two years, a dog year is equal to 10.5 human years. After that, each dog year equals 4 human years.
Expected Output:

def min_of_two(a, b):
    if a > b:
        return b
    return a

def min_of_three(a, b, c):
    min_ab = min_of_two(a, b)
    min_abc = min_of_two(min_ab, c)
    return min_abc

def min_of_three_v2(a, b, c):
    min_ab = a
    if a > b:
        min_ab = b
    min_abc = min_ab
    if min_ab > c:
        min_abc = c
    return min_abc

def min_of_three_v3(a, b, c):
    min_abc = a
    if min_abc > b:
        min_abc = b
    if min_abc > c:
        min_abc = c
    return min_abc
8

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên: Write a Python program to check whether an alphabet is a vowel or consonant. Go to the editor
Expected Output:

19. Viết một chương trình Python để in mẫu bảng chữ cái 'E'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên: Write a Python program to convert month name to a number of days. Go to the editor
Expected Output:

# if-elif-else is ok.
# nested if is hard to read
# if-elif-elif-elif-elif...-else is hard to read.
# hard to read == easy to have bugs, which is bad.

def min_abc_slower(a, b, c):
    if a > b:
        # a > b. This means min(a, b) == b
        if b > c:
            # b > c. This means min(c, min(a, b)) == c
            return c
        else:
            # b > c is False. This means b <= c.
            # So, min(c, min(a, b)) == b
            return b
    else:
        # a > b is False. This means a <= b.
        # So, min(a, b) = a
        if a > c:
            # a > c. This means min(c, min(a, b)) == c
            return c
        else:
            # a > c is False. This means a <= c
            # So, min(c, min(a, b)) == a
            return a
0

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên:Write a Python program to sum of two given integers. However, if the sum is between 15 to 20 it will return 20. Go to the editor

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên: Write a Python program to check a string represent an integer or not. Go to the editor
Expected Output:

# if-elif-else is ok.
# nested if is hard to read
# if-elif-elif-elif-elif...-else is hard to read.
# hard to read == easy to have bugs, which is bad.

def min_abc_slower(a, b, c):
    if a > b:
        # a > b. This means min(a, b) == b
        if b > c:
            # b > c. This means min(c, min(a, b)) == c
            return c
        else:
            # b > c is False. This means b <= c.
            # So, min(c, min(a, b)) == b
            return b
    else:
        # a > b is False. This means a <= b.
        # So, min(a, b) = a
        if a > c:
            # a > c. This means min(c, min(a, b)) == c
            return c
        else:
            # a > c is False. This means a <= c
            # So, min(c, min(a, b)) == a
            return a
1

Nhấp vào tôi để xem giải pháp mẫu

18. Viết một chương trình Python để in mẫu bảng chữ cái 'd'. Chuyển đến đầu ra dự kiến ​​của biên tập viên: Write a Python program to check a triangle is equilateral, isosceles or scalene. Go to the editor
Note :
An equilateral triangle is a triangle in which all three sides are equal.
A scalene triangle is a triangle that has three unequal sides.
An isosceles triangle is a triangle with (at least) two equal sides.
Expected Output:

# if-elif-else is ok.
# nested if is hard to read
# if-elif-elif-elif-elif...-else is hard to read.
# hard to read == easy to have bugs, which is bad.

def min_abc_slower(a, b, c):
    if a > b:
        # a > b. This means min(a, b) == b
        if b > c:
            # b > c. This means min(c, min(a, b)) == c
            return c
        else:
            # b > c is False. This means b <= c.
            # So, min(c, min(a, b)) == b
            return b
    else:
        # a > b is False. This means a <= b.
        # So, min(a, b) = a
        if a > c:
            # a > c. This means min(c, min(a, b)) == c
            return c
        else:
            # a > c is False. This means a <= c
            # So, min(c, min(a, b)) == a
            return a
2

Nhấp vào tôi để xem giải pháp mẫu

37. Viết một chương trình Python đọc hai số nguyên đại diện cho một tháng và ngày và in mùa cho tháng và ngày đó. Chuyển đến đầu ra dự kiến ​​của biên tập viên: Write a Python program that reads two integers representing a month and day and prints the season for that month and day. Go to the editor
Expected Output:

# if-elif-else is ok.
# nested if is hard to read
# if-elif-elif-elif-elif...-else is hard to read.
# hard to read == easy to have bugs, which is bad.

def min_abc_slower(a, b, c):
    if a > b:
        # a > b. This means min(a, b) == b
        if b > c:
            # b > c. This means min(c, min(a, b)) == c
            return c
        else:
            # b > c is False. This means b <= c.
            # So, min(c, min(a, b)) == b
            return b
    else:
        # a > b is False. This means a <= b.
        # So, min(a, b) = a
        if a > c:
            # a > c. This means min(c, min(a, b)) == c
            return c
        else:
            # a > c is False. This means a <= c
            # So, min(c, min(a, b)) == a
            return a
3

Nhấp vào tôi để xem giải pháp mẫu

38. Viết một chương trình Python để hiển thị dấu hiệu chiêm tinh cho ngày sinh đã cho. Chuyển đến đầu ra dự kiến ​​của biên tập viên: Write a Python program to display astrological sign for given date of birth. Go to the editor
Expected Output:

# if-elif-else is ok.
# nested if is hard to read
# if-elif-elif-elif-elif...-else is hard to read.
# hard to read == easy to have bugs, which is bad.

def min_abc_slower(a, b, c):
    if a > b:
        # a > b. This means min(a, b) == b
        if b > c:
            # b > c. This means min(c, min(a, b)) == c
            return c
        else:
            # b > c is False. This means b <= c.
            # So, min(c, min(a, b)) == b
            return b
    else:
        # a > b is False. This means a <= b.
        # So, min(a, b) = a
        if a > c:
            # a > c. This means min(c, min(a, b)) == c
            return c
        else:
            # a > c is False. This means a <= c
            # So, min(c, min(a, b)) == a
            return a
4

Nhấp vào tôi để xem giải pháp mẫu

39. Viết một chương trình Python để hiển thị dấu hiệu của cung hoàng đạo Trung Quốc cho năm nhất định mà bạn sinh ra. Chuyển đến đầu ra dự kiến ​​của biên tập viên: Write a Python program to display the sign of the Chinese Zodiac for given year in which you were born. Go to the editor
Expected Output:

# if-elif-else is ok.
# nested if is hard to read
# if-elif-elif-elif-elif...-else is hard to read.
# hard to read == easy to have bugs, which is bad.

def min_abc_slower(a, b, c):
    if a > b:
        # a > b. This means min(a, b) == b
        if b > c:
            # b > c. This means min(c, min(a, b)) == c
            return c
        else:
            # b > c is False. This means b <= c.
            # So, min(c, min(a, b)) == b
            return b
    else:
        # a > b is False. This means a <= b.
        # So, min(a, b) = a
        if a > c:
            # a > c. This means min(c, min(a, b)) == c
            return c
        else:
            # a > c is False. This means a <= c
            # So, min(c, min(a, b)) == a
            return a
5

Nhấp vào tôi để xem giải pháp mẫu

40. Viết một chương trình Python để tìm trung bình của ba giá trị. Chuyển đến đầu ra dự kiến ​​của biên tập viên: Write a Python program to find the median of three values. Go to the editor
Expected Output:

# if-elif-else is ok.
# nested if is hard to read
# if-elif-elif-elif-elif...-else is hard to read.
# hard to read == easy to have bugs, which is bad.

def min_abc_slower(a, b, c):
    if a > b:
        # a > b. This means min(a, b) == b
        if b > c:
            # b > c. This means min(c, min(a, b)) == c
            return c
        else:
            # b > c is False. This means b <= c.
            # So, min(c, min(a, b)) == b
            return b
    else:
        # a > b is False. This means a <= b.
        # So, min(a, b) = a
        if a > c:
            # a > c. This means min(c, min(a, b)) == c
            return c
        else:
            # a > c is False. This means a <= c
            # So, min(c, min(a, b)) == a
            return a
6

Nhấp vào tôi để xem giải pháp mẫu

41. Viết một chương trình Python để có được ngày hôm sau của một ngày nhất định. Chuyển đến đầu ra dự kiến ​​của biên tập viên: Write a Python program to get next day of a given date. Go to the editor
Expected Output:

# if-elif-else is ok.
# nested if is hard to read
# if-elif-elif-elif-elif...-else is hard to read.
# hard to read == easy to have bugs, which is bad.

def min_abc_slower(a, b, c):
    if a > b:
        # a > b. This means min(a, b) == b
        if b > c:
            # b > c. This means min(c, min(a, b)) == c
            return c
        else:
            # b > c is False. This means b <= c.
            # So, min(c, min(a, b)) == b
            return b
    else:
        # a > b is False. This means a <= b.
        # So, min(a, b) = a
        if a > c:
            # a > c. This means min(c, min(a, b)) == c
            return c
        else:
            # a > c is False. This means a <= c
            # So, min(c, min(a, b)) == a
            return a
7

Nhấp vào tôi để xem giải pháp mẫu

42. Viết chương trình Python để tính tổng và trung bình của N Số nguyên (nhập từ người dùng). Đầu vào 0 để kết thúc. Chuyển đến biên tập viên Write a Python program to calculate the sum and average of n integer numbers (input from the user). Input 0 to finish. Go to the editor

Nhấp vào tôi để xem giải pháp mẫu

43. Viết chương trình Python để tạo bảng nhân (từ 1 đến 10) của một số. Chuyển đến đầu ra dự kiến ​​của biên tập viên: Write a Python program to create the multiplication table (from 1 to 10) of a number. Go to the editor
Expected Output:

# if-elif-else is ok.
# nested if is hard to read
# if-elif-elif-elif-elif...-else is hard to read.
# hard to read == easy to have bugs, which is bad.

def min_abc_slower(a, b, c):
    if a > b:
        # a > b. This means min(a, b) == b
        if b > c:
            # b > c. This means min(c, min(a, b)) == c
            return c
        else:
            # b > c is False. This means b <= c.
            # So, min(c, min(a, b)) == b
            return b
    else:
        # a > b is False. This means a <= b.
        # So, min(a, b) = a
        if a > c:
            # a > c. This means min(c, min(a, b)) == c
            return c
        else:
            # a > c is False. This means a <= c
            # So, min(c, min(a, b)) == a
            return a
8

Nhấp vào tôi để xem giải pháp mẫu

44. Viết một chương trình Python để xây dựng mẫu sau, sử dụng số vòng lặp lồng nhau. Chuyển đến đầu ra dự kiến ​​của biên tập viên: Write a Python program to construct the following pattern, using a nested loop number. Go to the editor
Expected Output:

# if-elif-else is ok.
# nested if is hard to read
# if-elif-elif-elif-elif...-else is hard to read.
# hard to read == easy to have bugs, which is bad.

def min_abc_slower(a, b, c):
    if a > b:
        # a > b. This means min(a, b) == b
        if b > c:
            # b > c. This means min(c, min(a, b)) == c
            return c
        else:
            # b > c is False. This means b <= c.
            # So, min(c, min(a, b)) == b
            return b
    else:
        # a > b is False. This means a <= b.
        # So, min(a, b) = a
        if a > c:
            # a > c. This means min(c, min(a, b)) == c
            return c
        else:
            # a > c is False. This means a <= c
            # So, min(c, min(a, b)) == a
            return a
9

Nhấp vào tôi để xem giải pháp mẫu

Nhiều hơn nữa sẽ đến!

Không gửi bất kỳ giải pháp nào cho các bài tập trên tại đây, nếu bạn muốn đóng góp vào trang tập thể dục phù hợp.

Làm thế nào lồng nhau nếu

Có hai cách chính để tạo ra một tuyên bố nếu lồng nhau.Tùy chọn đầu tiên là đặt câu lệnh IF bên trong khối mã IF.Tùy chọn khác là đặt câu lệnh IF trong mã khác của câu lệnh IF/ELSE.Python đánh giá câu tuyên bố Nested này khi điều kiện của câu lệnh IF trước là đúng.place the if statement in the else code of an if/else statement. Python evaluates this nested if statement when the condition of the preceding if statement is True .

Được lồng nếu

Lồng nhau nếu các câu lệnh đề cập đến một câu lệnh IF bên trong một câu lệnh khác.Nói cách khác, vâng.Python cho phép bạn làm tổ nếu các câu lệnh trong các câu lệnh.Python does allow you to nest if statements within if statements.

3 loại tuyên bố điều kiện Python là gì?

Python cung cấp bốn tuyên bố có điều kiện ...
Nếu tuyên bố ..
tuyên bố if-else ..
Tuyên bố của Elif ..
Nested If và if-Else tuyên bố ..
Ladder Elif ..

Chúng ta có thể sử dụng nếu bên trong Elif không?

Cú pháp của nếu ... Elif ... khác nó cho phép chúng tôi kiểm tra nhiều biểu thức.Nếu điều kiện nếu là sai, nó sẽ kiểm tra điều kiện của khối Elif tiếp theo, v.v.Nếu tất cả các điều kiện là sai, cơ thể khác được thực thi.It allows us to check for multiple expressions. If the condition for if is False , it checks the condition of the next elif block and so on. If all the conditions are False , the body of else is executed.