Python Forum

Full Version: max_minus_min_abs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have to come up with a code for max_minus_min_abs but I am confused on where to enter the numbers I am passing through. This is what I am using

import math
 
def max_minus_min_abs(first_num, second_num):
    return max(first_num, second_num) - min(first_num, second_num)
    print(max_minus_min_abs(15, 4))
You don't seem to need to import math, unless you are doing something else as well.

def max_minus_min_abs(first_num, second_num):
    return max(first_num, second_num) - min(first_num, second_num)

# split returns a list of strings here
nums = input('Enter 2 numbers separated by a space. ').split(' ')
# use int() to make an integer from the string
result = max_minus_min_abs(int(nums[0]), int(nums[1])
print(f'the difference between {nums[0]} and {nums[1]} is', result)
Like this:
import math
  
def max_minus_min_abs(first_num, second_num):
    return max(first_num, second_num) - min(first_num, second_num)

print(max_minus_min_abs(15, 4))
Indentation is used to block code in Python. The indenting in your code made the print statement part of the max_minus_min_abs function.
def max_minus_min_abs(first_num, second_num):
    return max(first_num, second_num) - min(first_num, second_num)
 
# split returns a list of strings here
nums = input('Enter 2 numbers separated by a space. ').split(' ')
# use int() to make an integer from the string
result = max_minus_min_abs(int(nums[0]), int(nums[1]))
print(f'the difference between {nums[0]} and {nums[1]} is', result)