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
Show 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