Question

In: Computer Science

The question asks to modify this for an unspecified number of months. This is the python...

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()

Solutions

Expert Solution

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)


Related Solutions

Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
CODE IN PYTHON 1. Write a program that asks the user for the number of slices...
CODE IN PYTHON 1. Write a program that asks the user for the number of slices of pizza they want to order and displays the total number of dollar bills they owe given pizza costs 3 dollars a slice.  Note: You may print the value an integer value. 2. Assume that y, a and b have already been defined, display the value of x: x =   ya+b    3. The variable start_tees refers to the number of UD T-shirts at the start...
Write a Python program tat asks the user for the number of equations (n) to be...
Write a Python program tat asks the user for the number of equations (n) to be solved and fill the coefficient matrix A and the right matrix b with random integers. Limit the values of the elements of A to numbers between 0 and 50 and elements of b to 0 and 500. Lastly, put the matrices into an Excel file.
Assignment #2 will be the construction of a program that reads in an unspecified number of...
Assignment #2 will be the construction of a program that reads in an unspecified number of integers from standard input, performs some calculations on the inputted numbers, and outputs the results of those calculations to standard output. The numbers could be delimited by any kind of whitespace, i.e. tabs, spaces, and lines (Note that if you use the Scanner class, you do not have to worry about these delimiters. They will be taken care of). Your program will continue to...
PYTHON: Write a program that asks the user to enter a 10-character telephone number in the...
PYTHON: Write a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD, the application should display 555-438-3663. This is my code, but I cannot figure out where to go from here. #set new number new_number = "" #split number split_num = phone.split("-") for char in split_num[1:2]:...
Write a python program which asks the user to enter a positive number that is greater...
Write a python program which asks the user to enter a positive number that is greater than 30 called, “num2” and then does the following: o 1) Print all numbers between 1 and “num2” that are divisible by 2 and 3. o 2) Print all numbers between 1 and “num2” that are either divisible by 6 or 7. o 3) Print all numbers between 1 and “num3” that is not divisible by 5
Python Programming Question: Objective: Sum of Numbers. Design a program with a loop that asks the...
Python Programming Question: Objective: Sum of Numbers. Design a program with a loop that asks the user to enter a series of positive numbers. The user should enter "0" (zero) to signal the end of the series. After all the positive numbers have been entered, the program should display their sum. The program should display the following: • Contain proper indentation. • Comments (Blocks or Lines). • Good Variables initializations. • Good questions asking for the parameters of the code....
Convert the following text into a code format: The program reads an unspecified number of integers...
Convert the following text into a code format: The program reads an unspecified number of integers until a zero is entered. While the program reads each number it counts the number of positive numbers and the number of negative numbers that have been entered and sum up the values of the numbers entered. After the user enters zero, the program computes the average of the input values, not counting the zero. At the end, the program displays the average, and...
Python question Recall that a prime number is an integer that is only divisible by 1...
Python question Recall that a prime number is an integer that is only divisible by 1 and itself. For example, numbers 2, 3, 5, 7, 13, 19 are prime, whereas 4, 10, 12, 100 are not. Also, recall that factors are the numbers you multiply to get another number. For example, 24 has 8 factors: 1, 2, 3, 4, 6, 8, 12, and 24. As you know, any number can be factorized into several (possibly repeating) prime factors. For instance,...
PYTHON Modify the program in section Ask the user for a first name and a last...
PYTHON Modify the program in section Ask the user for a first name and a last name of several people.  Use a loop to ask for user input of each person’s first and last names  Each time through the loop, use a dictionary to store the first and last names of that person  Add that dictionary to a list to create a master list of the names  Example dictionary: aDict = { "fname":"Douglas", "name":"Lee" } ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT