Write a program that has a user defined function NOTES to accept 2 numbers as parameters, if number 1 is less than number 2 then numbers are swapped and returned, i.e., number 2 is returned in place of number 1 and number 1 is reformed in place of number 2 , otherwise the same order is returned.
Question
Write a program that has a user defined function NOTES to accept 2 numbers as parameters, if number 1 is less than number 2 then numbers are swapped and returned, i.e., number 2 is returned in place of number 1 and number 1 is reformed in place of number 2 , otherwise the same order is returned.
✨ Free AI Tools for You
Powered by Chatterbot AI
Answer
Here is a simple Python program that includes a user-defined function called swap_if_needed
to accept 2 numbers as parameters, swap them if the first number is less than the second number, and return them in the appropriate order:
# Function to accept 2 numbers and swap them if the first is less than the second
def swap_if_needed(num1, num2):
if num1 < num2:
# Swapping the numbers
return num2, num1
else:
# Returning the numbers in the same order
return num1, num2
# Taking input from the user
number1 = float(input("Enter the first number: "))
number2 = float(input("Enter the second number: "))
# Calling the function and storing the result
result1, result2 = swap_if_needed(number1, number2)
# Displaying the result
print("After swap (if needed):")
print("First Number:", result1)
print("Second Number:", result2)
Explanation:
The function
swap_if_needed
checks ifnum1
is less thannum2
.If the condition is true, it swaps the numbers and returns them.
If the condition is false, it returns the numbers in the same order.
The user is prompted to input two numbers.
The function is called with these user inputs, and the returned values (in the appropriate order) are stored in
result1
andresult2
.Finally, the results are printed.
India's 1st AI Doubt Solver for CBSE, JEE, and NEET
Ask a Question for Freeand then it's just ₹212 a month