鱼C论坛

 找回密码
 立即注册
查看: 752|回复: 2

[技术交流] 课后作业补充

[复制链接]
发表于 2018-1-8 10:30:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Write a program that uses nested loops to collect data and calculate the average rainfall
over a period of years. The program should first ask for the number of years. The outer loop
will iterate once for each year. The inner loop will iterate twelve times, once for each month.
Each iteration of the inner loop will ask the user for the inches of rainfall for that month.
After all iterations, the program should display the number of months, the total inches of
rainfall, and the average rainfall per month for the entire period.


大家可以试着翻译然后写下代码。我把我的代码一会儿传上。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-1-8 10:38:11 | 显示全部楼层
  1. # 获得需要收集降雨量的年数。
  2. collect_years = int(input('Enter the number of collected years: '))

  3. # 设定一个flag
  4. yearly_rainfall = 0

  5. # 设定年数的一个循环
  6. for year in range(collect_years):
  7.     print('For year', year + 1,',')
  8.     for each_month in range(12):
  9.         print('Enter month', each_month + 1, 'rainfall', end='')
  10.         monthly_rainfall = float(input(' : '))
  11.         yearly_rainfall += monthly_rainfall
  12.         yearly_average_rainfall = yearly_rainfall / collect_years / 12
  13.     monthly_average_rainfall = yearly_rainfall / 12/(year + 1)
  14.   #  print('The average rainfall in year', year + 1, 'is:', \
  15.   #            format(monthly_average_rainfall, '.2f'))
  16. # 输出总的降雨量
  17. print('The total rainfall in the entire period is:', yearly_rainfall)

  18. # 输出年平均降雨量
  19. print('The average rainfall in the entire period is:', \
  20.       format(yearly_average_rainfall, '.2f'))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-8 10:40:42 | 显示全部楼层
现在我有一个问题就是, 怎么样输入第12 个月以后, 可以看到本年的年平均降雨量?我写上去的,感觉不对。请高手修改一下。谢谢了。
#  print('The average rainfall in year', year + 1, 'is:', \
  #            format(monthly_average_rainfall, '.2f'))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 20:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表