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

Homework Answers

Answer #1
# 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:
        magnitude=float(input(("Please enter magnitude of earthquake",number)))
        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
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write the functions which are called in the main function given. YOU MAY ASSUME THAT THE...
Write the functions which are called in the main function given. YOU MAY ASSUME THAT THE USER ENTERS A VALID INTEGER. printDouble should accept one value and should print the value of that number times 2. printEndMessage should not accept any values and should print a message indicating that the program has finished. def main(): num1 = int(input("Please enter your number ")) printDouble(num1) printEndMessage() main()
Create a Python main program which calls two functions enterNum and calcResult and accomplishes the following:...
Create a Python main program which calls two functions enterNum and calcResult and accomplishes the following: 1. The main program calls the function enterNum 3 times. The first time enterNum is called, the main program stores the returned output in the variable xx. The second time, the returned output is stored in the variable yy and the third time in zz. 2. enterNum asks the user to enter a floating point number. This function has no input arguments and returns...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Description: The purpose of this assignment is to practice writing code that calls functions, and contains...
Description: The purpose of this assignment is to practice writing code that calls functions, and contains loops and branches. You will create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. General Comments: Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement an...
(8 marks) Write a program to ask user to input an integer and display the special...
Write a program to ask user to input an integer and display the special pattern accordingly. REQUIREMENTS The user input is always correct (input verification is not required). Your code must use loop statements (for, while or do-while). Your program should use only the following 3 output statements, one of EACH of the followings: System.out.print("-"); // print # System.out.print("+"); // print + System.out.println(); // print a newline Your code must work exactly like the following example (the text in bold...
ALL IN PYTHON PLEASE Problem 4 Write a program to compute the area of a circle...
ALL IN PYTHON PLEASE Problem 4 Write a program to compute the area of a circle some 'n' times. You must accept n and r from the user. Area is calculated using (22/7.0)*r*r - where r is the radius. Implement using value-returning functions. Hint: create a function to calculate area taking r as a parameter. In the main() function, ask for n and create a loop where you input r and invoke the area function n times. Rubric: Correct use...
Write a program that asks the user to enter two integer numbers. the main function then...
Write a program that asks the user to enter two integer numbers. the main function then passes these numbers to a function named exp that calculates the multiplication of these numbers and print the exponential value of the result of multiplication
Python code only! (Do not include breaks or continue functions) Write a program that asks the...
Python code only! (Do not include breaks or continue functions) Write a program that asks the user to enter the amount that they budgeted for the month. A loop should then prompt the user to enter their expenses, one at a time and to enter 0 to quit. When the loop finishes, the program should display the the amount of budget left. (a negative number indicates the user is over budget and a positive number indicates the user is under...
In C++ write a program for a multiplication table It must Prompt the user for two...
In C++ write a program for a multiplication table It must Prompt the user for two integers between 1 and 20 (inclusive) • also must Calculates and displays the multiplication table in a well-formatted output The table must include a label for each row and column The program must follow the requirements: • Validates user input, displaying an error message and prompting to user to enter another integer if the input is invalid, and repeating it as many times as...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...