微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

打印算术平均值,最小和最大年龄

如何解决打印算术平均值,最小和最大年龄

这个 Python 程序应该读取出生年份,直到输入数字零。
然后程序应该打印出平均年龄以及最小和最大的年龄。

我需要两件事的帮助。

  1. 当输入年份为 -N 时打印“年龄不合理,请再试一次”,例如“-45”
  2. 在我退出循环时打印结果,即算术平均值、最年轻和最老的。 这意味着当我输入 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,enter 0: 1845
Unreasonable age,please try again
Average age is  72.0 years old.
The younges is 72 years old,enter 0: 1995
Average age is  48.5 years old.
The younges is 25 years old,enter 0: 2005
Average age is  37.333333333333336 years old.
The younges is 15 years old,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,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,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 ","years old.")
    # Print "The younges is ",min_year," and the oldest is ",max_year
    print("The youngest is","years old,",max_year,"years old.")

# Done! :-)

解决方法

代码的工作原理是简单地取消最后两个打印语句的缩进并取消对 if …: break 的注释:

# Initial value (has not started counting yet)
number_year = 0
# Initial value (has not started counting yet)
sum_year = 0
# No maximum age yet
max_year = 0
# Well increased minimum value (age) to start with
min_year = 110
# Only as start value for the sake of the loop start!
input_year = -1

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: "))
    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 < 0 or age > 110 and age != 2020:
        print("Unreasonable age,please try again")
    # else
    else:
        if input_year > 0:
            number_year += 1
            sum_year += age
            if age < min_year:
                ### New minimum age found
                min_year = age
            if age > max_year:
                ### New maximum age found
                max_year = age

    if input_year == 0:
        break

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

我还删除了不必要的评论。但是,只需使用列表就可以大大简化您的代码:

ages = []

while True: # infinite loop - we will exit by break-ing when age is 0
    age = 2020 - int(input("Please enter birth year (0 to exit)"))

    if age == 2020: # user entered a 0 - exit loop
        break

    if age < 0 or age > 110:
        print("Unreasonable age,please try again")
        continue # directly go to next loop
    
    ages.append(age) # will only get appended if the condition above was false because of the continue

if ages: # ages list is not empty
    print("Average age is",sum(ages) / len(ages),"years old")
    print("The youngest is",min(ages),"old,and the oldest is",max(ages),"old")
else:
    print("No ages entered - cannot print mean,min and max age - exiting")
,

您可以将所有年龄存储在一个列表中,然后根据需要进行数学计算。

# initialize your list
ages = []

# run your code here,adding the age value with each valid input
# this can be done right before finishing the loop,after the last if statement
...
while input_year != 0:
    ...
    ages.append(age)

# at the end,do the required calculations
average_age = np.mean(ages)
min_age = np.min(ages)
max_age = np.max(ages)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。