如何使用 re.sub

如果我明白你想要什么(计算这段时间内降雨量的绝对平均值),这应该可以解决问题:


number_of_months = 3

years_in_period = int(input("Please enter the number of years in the period. \n"))


total_rain = 0


for year in range(years_in_period):

    yearly_rain = 0

    print('Year', year+1) 

    print('−−−−−−−−−−−−−−−−−')


    for month in range(number_of_months):

        print('Month', month+1, end='')

        monthly_rain = float(input("Please enter rainfall for this month: \n"))


        yearly_rain += monthly_rain


        total_rain += monthly_rain


        average_yearly_rainfall = yearly_rain / number_of_months


    print("Average yearly rainfall of year ", year+1, " is ", average_yearly_rainfall)

    print("Year total rain is", yearly_rain)

    print()



total_months = years_in_period * number_of_months

print("Absolute average of rain/month was", total_rain/total_months)

print("Absolute average of rain/year was", total_rain/years_in_period)


天涯尽头无女友
浏览 178回答 1
1回答

三国纷争

使用与您为结尾所做的相似的模式,并将它们串在一起。:test = re.sub(r'.* 159 ', '', re.sub(r' 265.*$', '', test))或者,您可以使用单个模式和re.findall:test = re.findall(r'^.*159 ([A-Z]+) 265.*$', test)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python