Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
max_minus_min_abs
#1
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))
Gribouillis write Sep-17-2023, 08:17 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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)
Reply
#3
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.
Reply
#4
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)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020