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()
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...
(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...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
In C programming, Thank you Write a program to compute the area of a circle. You...
In C programming, Thank you Write a program to compute the area of a circle. You have to write a complete “C” program that compiles and runs in Codeblocks. Program requirements: 1. Declare the variables needed: radius (r) and area (yourLastName). 2. Calculate and print the area of each circle. 3. The program MUST prompt the user for a new radius (r) over and over until the user types -1. 5. Use the type double for all decimal numbers. 6....
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
A) write a program the computes nx and store the result into y You can use...
A) write a program the computes nx and store the result into y You can use y = Math.pow( Mantissa, exponent) Requirements: Besides main() your program must have one method with two parameters, one double and one int n and x are positive numbers read from the keyboard if the user makes an entry that does not meet this criteria, the user must be given to opportunity retry the entry the user must be able to quit prior to entering...
Please provide answer in the format that I provided, thank you Write a program that prompts...
Please provide answer in the format that I provided, thank you Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must...
**C code only In this part, you will write a simple user-defined function that prints a...
**C code only In this part, you will write a simple user-defined function that prints a message. This function does not require any parameters or return value. The purpose is simply to get you started writing functions.Open repl project Lab: User-Defined Functions 1. Write a program that calls a function. This function should do the following: 1.Ask the user their name 2. Print a "hello" message that includes the user's name Example output: Please enter your name: ​Mat Hello, Mat!...