Question

# In this assignment you will write a program that compares the relative strengths of two earthquakes,...

In this assignment you will write a program that compares the relative strengths of two earthquakes, given their magnitudes using the moment magnitude scale.

Earthquakes

The amount of energy released during an earthquake -- corresponding to the amount of shaking -- is measured using the "moment magnitude scale". We can compare the relative strength of two earthquakes given the magnitudes m1 and m2 using this formula:

f=10^1.5(m1−m2)

If m1>m2, the resulting value f tells us how many times stronger m1 was than m2; if the opposite is true, then the reciprocal of f (1/f) tells us how many times stronger m2 was than m1.

Program Flow

Your Python program will ask the user to input two earthquake magnitudes, validating that each value entered is positive and looping if it is not. Once the magnitudes are validated, you will decide which earthquake had the higher magnitude, then use the formula above to compute f. You will output a message indicating which magnitude was stronger, and how many times stronger it was than the second magnitude. You must print three values in this output: the magnitude of the stronger earthquake, the value of f, and the magnitude of the weaker earthquake.

After printing the output, ask the user if they want to try again, and loop the program as long as they answer "1" (for "yes").

Functions

You must break your program up into the following functions:

1. get_magnitude(number): this function asks the user to input an earthquake magnitude. The number parameter is an integer that identifies which earthquake (1 or 2) the user is being asked about; you must include the earthquake number in your output. The user will enter the magnitude, which you must validate is positive; note that magnitudes do not need to be whole numbers. Once you have a positive value from the user, return it.

For example, calling get_magnitude(1) should print "Please enter the magnitude for earthquake 1:". If the user types in "5", then the value 5 would be returned from the function.

2. compare_magnitudes(magnitude1, magnitude2): this function takes two magnitude values as parameters. It calculates and returns f using the formula above. It does not print or gather input from the user. Note that the value that gets returned is from the perspective of magnitude 1; if magnitude 1 is smaller than magnitude 2, the value you compute and return will be less than 1, indicating that the first earthquake was weaker than the second. Seek help and do not continue if you don't understand what this means.

3. get_run_again(): this function asks the user if they want to compare 2 more earthquakes; it is called from the main to determine if the main should loop again. The user must enter a 1 to continue; any other value means "quit".

This function returns the value True if and only if the user enters a 1; any other value causes the function to return False. You cannot return a string, integer, or any other value from this function; only a True or False.

4. "___main__" block using an if statement: the main "drives" the application, by performing the steps in the "Program Flow" section above. It calls get_magnitude twice and stores the results of the function in variables, then determines which variable is larger and calls compare_magnitudes, passing the larger magnitude as the first parameter. It then prints the results of the comparison, always printing the larger magnitude first.

The main then calls get_run_again. If the user wants to run the program again, the main loops; otherwise it terminates.

This function cannot call input(). In addition, it can only call print three times.

Example Output

```User input is in `_italics_`.

Please enter the magnitude of earthquake 1: *-5*
Please enter the magnitude of earthquake 1: *5.8*
Please enter the magnitude of earthquake 2: *7.5*

An earthquake of magnitude 7.5 is 354.8 times more powerful than an earthquake of magnitude 5.8.
Try again? Type 1 for yes: *1*

Please enter the magnitude of earthquake 1: *7*
Please enter the magnitude of earthquake 2: *-6*
Please enter the magnitude of earthquake 2: *6*

An earthquake of magnitude 7.0 is 31.6 times more powerful than an earthquake of magnitude 6.0.

Try again? Type 1 for yes: *0*

Bye!
```

NOTE: compare_magnitudes should not do any rounding or truncation of its own. The final output of the program should be rounded to 1 decimal using {:.1f}, but that is a decision made by the "main"; compare_magnitudes itself should do no rounding/truncation

``````# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
def get_magnitude(number):
while 1:
if magnitude > 0:
break
return magnitude

def compare_magnitudes(magnitude1,magnitude2):
if(magnitude1>magnitude2):
f=pow(10,1.5*(magnitude1-magnitude2))
else:
f=pow(10,1.5*(magnitude2-magnitude1))
return f

def get_run_again():
choice =int(input("Try again ? Type 1 for yes"))
return choice

while 1:
number=1
magnitude1=get_magnitude(number)
number=number+1
magnitude2=get_magnitude(number)
result=compare_magnitudes(magnitude1,magnitude2)
print("result is:","{0:.1f}".format(result))
choice=get_run_again()
if choice != 1:
break
``````

#### Earn Coins

Coins can be redeemed for fabulous gifts.