In: Computer Science
The question asks to modify this for an unspecified number of months. This is the python code I have, please advise next steps
def main():
months = range(1,13)
rain_month = 0.0
rain_per_month = []
month = 0
count = 1
amount = 0.0
total_rain = 0.0
average_rain = 0.0
for month in months:
print("Rain for month "+str(count)+":")
rain_amount = get_rainfall()
rain_per_month.append(rain_amount)
count = count + 1
total_rain = calc_total_rain(rain_per_month)
average_rain = rain_average(total_rain)
print("Total Rain:", total_rain)
print("Average Rain:", average_rain)
def get_rainfall():
rain = 0.0
value_good = False
while not(value_good):
try:
rain = float(input("Enter rain for month: "))
except:
print("Invalid input")
value_good = False
else:
if rain >= 0:
value_good= True
else:
print("Rainfall must be positive")
value_good = False
return rain
def calc_total_rain(months):
total = 0.0
amount = 0.0
for amount in months:
total = amount + total
return total
def rain_average(tot):
months = 12
ave = 0.0
ave = tot/12
return ave
main()
Answer:
Code:
Output:
I clearly mentioned comments for lines
Raw Code:
months = int(input("Please Specify Number of Months: "))# Asking
user for unspecified Months
def main():
rain_month = 0.0
rain_per_month = []
month = 0
count = 1
amount = 0.0
total_rain = 0.0
average_rain = 0.0
for month in range(1,months+1):
print("Rain for month
"+str(count)+":")
rain_amount = get_rainfall()
rain_per_month.append(rain_amount)
count = count + 1
total_rain = calc_total_rain(rain_per_month)
average_rain = rain_average(total_rain)
print("Total Rain:", total_rain)# printing total
rain
print("Average Rain:", average_rain)#printing Average
rain
def get_rainfall():
rain = 0.0
value_good = False
while not(value_good):
try:
rain =
float(input("Enter rain for month: "))
except:
print("Invalid
input")#if user not gives output prints invalid
value_good =
False
if rain >= 0:
value_good=
True
else:
print("Rainfall
must be positive")#if user enters negative rain fall
value_good =
False
return rain
def calc_total_rain(months):
total = 0.0
amount = 0.0
for amount in months:
total = amount + total# Calculating
total rainfall
return total
def rain_average(tot):
ave = 0.0
ave = tot/months# Calculating Average
return ave
main()# calling Main
Thank You
please do vote(Like)