You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将使用elif语句的月份天数查询Python程序改写为嵌套if实现

Converting an elif-based month program to nested if statements

Hey there! Let's work through converting your month program from using elif to nested if statements. First, I spotted a couple of small issues in your original code that I fixed in the solution below:

  • You had January and February swapped (month 1 was printing February, and vice versa)
  • Some month names had inconsistent capitalization (e.g., "october" → "October")

The core idea behind converting elif to nested if is that each elif is essentially an if nested inside the else of the previous condition. We'll start by first validating that the input is a valid month (1-12), then nest our checks for each individual month inside that.

Here's the rewritten program using nested if statements:

month_num = int(input("Enter the number of a month (Jan = 1) : "))

# First validate the input is a valid month
if 1 <= month_num <= 12:
    # Nested if checks for each month
    if month_num == 1:
        print(month_num, "is January, and has 31 days.")
    else:
        if month_num == 2:
            print(month_num, "is February, and has 29 days.")  # Note: Add leap year logic later if needed!
        else:
            if month_num == 3:
                print(month_num, "is March, and has 31 days.")
            else:
                if month_num == 4:
                    print(month_num, "is April, and has 30 days.")
                else:
                    if month_num == 5:
                        print(month_num, "is May, and has 31 days.")
                    else:
                        if month_num == 6:
                            print(month_num, "is June, and has 30 days.")
                        else:
                            if month_num == 7:
                                print(month_num, "is July, and has 31 days.")
                            else:
                                if month_num == 8:
                                    print(month_num, "is August, and has 31 days.")
                                else:
                                    if month_num == 9:
                                        print(month_num, "is September, and has 30 days.")
                                    else:
                                        if month_num == 10:
                                            print(month_num, "is October, and has 31 days.")
                                        else:
                                            if month_num == 11:
                                                print(month_num, "is November, and has 30 days.")
                                            else:  # Only valid remaining value is 12
                                                print(month_num, "is December, and has 31 days.")
else:
    print(month_num, "is not a valid number")

A quick side note: While nested if gets the job done here, your original elif approach is actually more concise and readable for this type of sequential condition checking. This version strictly follows the nested if structure you requested though!

内容的提问来源于stack exchange,提问作者Joseph

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 20:54:09