From 24b3d3d847afd4452d406e12a7b23b83455e4736 Mon Sep 17 00:00:00 2001 From: acsoneye Date: Thu, 24 Oct 2024 22:38:09 +0800 Subject: [PATCH] Update q1.py --- src/q1.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/q1.py b/src/q1.py index 3153622..fe17bf2 100644 --- a/src/q1.py +++ b/src/q1.py @@ -13,3 +13,24 @@ def swap(x, y): # Invoke the function "swap" using the following scenarios: # - "Apple", 10 # - 9, 17 + +def swap(x, y): + """ + Swap the values of x and y using only x and y as variables. + - Return -1 if x and y are not numeric. + - Print the swapped values if both x and y are numeric. + """ + # Check if both x and y are numeric + if not (isinstance(x, (int, float)) and isinstance(y, (int, float))): + return -1 + + # Swap the values + x, y = y, x + + # Print the swapped values + print(f"Swapped values: x = {x}, y = {y}") + return + +# Task 2: Invoke the function with specified scenarios +print(swap("Apple", 10)) # Scenario 1 +print(swap(9, 17)) # Scenario 2