LAB: Max magnitude. Sections: 2.8, 4.2, 6.7.
Write a function max_magnitude() with two integer input parameters that returns the largest magnitude value. Use the function in a program that takes two integer inputs, and outputs the largest magnitude value.
Your program must define and call the following function:
def max_magnitude(user_val1, user_val2)
Ex: If the inputs are:
5 7
the function returns:
7
Ex: If the inputs are:
-8 -2
the function returns:
-8
****IN PYTHON******

Respuesta :

Answer:

The program written in python is as follows:

import math

def max_magnitude(user_val1, user_val2):

     if abs(user_val1)>abs(user_val2):

           return user_val1

     else:

           return user_val2

val1 = int(input("Value 1: "))

val2 = int(input("Value 2: "))

print(max_magnitude(val1,val2))

Explanation:

This line imports the math module in the program

import math

This line declares the function with two parameters

def max_magnitude(user_val1, user_val2):

The if condition gets the absolute value of both integers and compares them; The integer with greater magnitude is returned, afterwards

     if abs(user_val1)>abs(user_val2):

           return user_val1

     else:

           return user_val2

The main method starts here

The next two lines prompt user for input

val1 = int(input("Value 1: "))

val2 = int(input("Value 2: "))

This line gets the integer with higher magnitude

print(max_magnitude(val1,val2))

ACCESS MORE
EDU ACCESS
Universidad de Mexico