Function FindMinMax - Python Programming Exercise

In this exercise, you will develop a Python program with a function called "FindRange", where the user is prompted to input a minimum value (a number) and a maximum value (another number). This exercise is perfect for practicing function definition, input validation, and conditional statements in Python. By implementing this function, you will gain hands-on experience in handling function definitions, input validation, and conditional statements in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Function Findminmax

 Objective

Develop a Python program a function called "FindRange", where the user is prompted to input a minimum value (a number) and a maximum value (another number). It should be called like this:

FindRange(n1, n2)

The function will behave in the following way: Enter the minimum value: 5
Enter the maximum value: 3.5
Invalid input. The maximum value must be greater than or equal to 5.
Enter the maximum value: 7

 Example Python Exercise

 Copy Python Code
# Function to find the range, ensuring the maximum value is greater than or equal to the minimum value
def FindRange(n1, n2):
    # Prompt user to enter minimum value
    min_value = float(input(f"Enter the minimum value: {n1}\n"))

    # Prompt user to enter maximum value
    max_value = float(input(f"Enter the maximum value: {n2}\n"))

    # Check if the maximum value is greater than or equal to the minimum value
    while max_value < min_value:
        print(f"Invalid input. The maximum value must be greater than or equal to {min_value}.")
        max_value = float(input(f"Enter the maximum value: "))

    # Return the minimum and maximum values once valid
    print(f"The valid range is from {min_value} to {max_value}.")
    return min_value, max_value

# Example usage
n1 = 5  # Example minimum value
n2 = 3.5  # Example maximum value

# Call the function with the example values
FindRange(n1, n2)

 Output

python find_range.py
Enter the minimum value: 5
Enter the maximum value: 3.5
Invalid input. The maximum value must be greater than or equal to 5.
Enter the maximum value: 7
The valid range is from 5.0 to 7.0.

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Function Multiply & MultiplyRecursive

    In this exercise, you will develop a Python program with two functions, Multiply and MultiplyRecursive, to compute the product of two numbers by using addition. The f...

  •  Functions: Hello and Goodbye

    In this exercise, you will develop a Python program where the main function should look like this: SayHello() and SayGoodbye(). You must define the functions SayHello...

  •  Parameterized Function

    In this exercise, you will develop a Python program where the main function should look like this: SayHello("John") and SayGoodbye(). You must define the functions Sa...

  •  Value-Returning Function

    In this exercise, you will develop a Python program where the main function should define and call a sum function that accepts two integers as parameters and returns ...

  •  Value-Returning Function V2

    In this exercise, you will develop a Python program where the main function should look like this: def main(): print("\"Hello, how are you\" contains {0} spaces".form...

  •  Centered Text Function

    In this exercise, you will develop a Python function named "get_int" that displays the text received as a parameter, prompts the user for an integer number, repeats i...