You need to enable JavaScript to run this app. Python出生年份统计程序修复:实现非法年龄提示与退出后输出结果
优惠活动
大模型
产品
解决方案
定价
更多

Python出生年份统计程序修复:实现非法年龄提示与退出后输出结果

问题背景

该Python程序需要持续读取用户输入的出生年份,直到用户输入数字0后终止循环,最终输出所有有效年龄的算术平均值、最小年龄与最大年龄。

待修复问题
  • 当输入年份为负数(例如-45)或计算出的年龄超出合理范围时,需要打印Unreasonable age, please try again提示
  • 算术平均值、最小年龄、最大年龄的统计结果仅在输入0退出循环后打印,不需要每次输入后都输出
当前实际输出
Please type in birth year, to stop, enter 0: 1948
Average age is  72.0 years old.
The younges is 72 years old,  and the oldest is  72 years old.
Please type in birth year, to stop, enter 0: 1845
Unreasonable age, please try again
Average age is  72.0 years old.
The younges is 72 years old,  and the oldest is  72 years old.
Please type in birth year, to stop, enter 0: 1995
Average age is  48.5 years old.
The younges is 25 years old,  and the oldest is  72 years old.
Please type in birth year, to stop, enter 0: 2005
Average age is  37.333333333333336 years old.
The younges is 15 years old,  and the oldest is  72 years old.
Please type in birth year, to stop, enter 0: 0
Average age is  37.333333333333336 years old.
The youngest is 15 years old,  and the oldest is  72 years old.
预期输出效果
Please type in birth year, to stop, enter 0.
Year: 1998
Year: 1932
Year: 1887
Fail: Unreasonable age, please try again.
Year: 1987
Year: -77
Fail: Unreasonable age, please try again.
Year: 1963
Year: 0
Average age is 49 years old. The youngest is 21 years old, and the oldest is 87 years old.
原有问题代码
# set number_years to zero     ### Initial value (has not started counting yet)
number_year = 0
# set number_years to zero     ### Initial value (has not started counting yet)
sum_year = 0
# set sum_year to zero    ### No maximum age yet
max_year = 0
# set max_year to zero      ### Well increased minimum value (age) to start with
min_year = 110
# set input_year to minus 1 ### Only as start value for the sake of the loop start!
input_year = -1

# White input_year is not 0:
while input_year != 0:
    # print info and store input value to input_year, stop if 0 is entered
    input_year = int(input("Please type in birth year, to stop, enter 0: "))
    # let age be (2020 - input_year)
    age = (2020 - input_year)
    # To avoid beauty flaws with the printout "Unreasonable year ..."
    # when the final zero is entered, we must check that age is not 2020
    # which it is deceptive enough because 2020-0=2020

    # if age is less than zero or age is greater than 110 and age is not 2020:
    if age < 0 or age > 110 and age != 2020:
        # Print "Unreasonable age, please try again"
        print("Unreasonable age, please try again")
    # else
    else:
        # if input_year is greater than zero:
        if input_year > 0:
            # increase number_year with 1
            number_year += 1
            # let sum_year become sum_year + age
            sum_year = sum_year + age
            # if age is less than min_year:
            if age < min_year:
                # set min_year to age ### New minimum age found
                min_year = age
            # if age is bigger than max_year:
            if age > max_year:
                # set max_year to age ### New maximum age found
                max_year = age

    ## If the entered number was 0, exit the loop
    #if input_year == 0:
    #    break

    # Now just print the arithmetic average, the youngest and oldest
    # Print "Average age is ", sum_year / number_year, "year."
    print("Average age is ", sum_year / number_year, "years old.")
    # Print "The younges is ", min_year, " and the oldest is ", max_year
    print("The youngest is", min_year, "years old,", " and the oldest is ", max_year, "years old.")

# Done! :-)
修复说明

修改3处核心逻辑即可匹配预期效果:

  1. 调整提示语格式:开头只打印一次总提示,循环内输入提示改为Year:
  2. 修复不合理年龄的判断逻辑:原逻辑运算符优先级错误,同时输入0时跳过不合理判断
  3. 把统计结果打印的代码移动到循环外部,平均值按预期取整输出
修复后可运行代码
# 初始化计数、年龄总和、最大最小年龄变量
number_year = 0
sum_year = 0
max_year = 0
min_year = 110
input_year = -1

# 开头打印总提示
print("Please type in birth year, to stop, enter 0.")

while input_year != 0:
    input_year = int(input("Year: "))
    # 输入0直接退出循环,不做判断
    if input_year == 0:
        break
    age = 2020 - input_year
    # 判断年龄是否合理
    if age < 0 or age > 110:
        print("Fail: Unreasonable age, please try again.")
    else:
        number_year += 1
        sum_year += age
        if age < min_year:
            min_year = age
        if age > max_year:
            max_year = age

# 循环结束后打印统计结果,兼容无有效输入的场景
if number_year > 0:
    avg_age = round(sum_year / number_year)
    print(f"Average age is {avg_age} years old. The youngest is {min_year} years old, and the oldest is {max_year} years old.")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 00:42:03