From e50d627ad67edc7672bb17786eff6fe833faf4e9 Mon Sep 17 00:00:00 2001 From: bigfudge22 Date: Tue, 31 Dec 2024 13:55:12 +0200 Subject: [PATCH] function1 --- solutions/steps_to_floor.py | 58 +++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/solutions/steps_to_floor.py b/solutions/steps_to_floor.py index 620593a2e..3fbacb9a2 100644 --- a/solutions/steps_to_floor.py +++ b/solutions/steps_to_floor.py @@ -1,30 +1,56 @@ -def steps_to_floor(target_floor: int = None) -> int: +def steps_to_floor_core(target_floor: int) -> int: """ - Calculates the number of steps required to reach a given floor in a building, + Core function to calculate the number of steps required to reach a given floor in a building, assuming it takes 10 steps to move between consecutive floors. - If no argument is provided, the function will prompt the user for input. - Parameters: - target_floor: int (optional), the floor number to reach (positive, negative, or zero). + target_floor: int, the floor number to reach (positive, negative, or zero). - Returns -> int, the total number of steps required. - """ - if target_floor is None: - try: - target_floor = int(input("Enter the floor number you want to reach: ")) - except ValueError: - raise ValueError("Invalid input. Please enter an integer.") + Returns: + int: the total number of steps required. + + Raises: + AssertionError: If the target_floor is not an integer. - # Ensure the target floor is an integer + Examples: + >>> steps_to_floor_core(0) + 0 + >>> steps_to_floor_core(1) + 10 + >>> steps_to_floor_core(5) + 50 + >>> steps_to_floor_core(-3) + 30 + """ + # Defensive assertion to ensure valid input assert isinstance(target_floor, int), "Target floor must be an integer" # Calculate steps based on the absolute floor value return abs(target_floor) * 10 -if __name__ == "__main__": +def steps_to_floor(): + """ + Wrapper function to handle user input for steps_to_floor_core. + """ try: - print(f"Steps required: {steps_to_floor()}") - except Exception as e: + target_floor = int(input("Enter the floor number you want to reach: ")) + steps = steps_to_floor_core(target_floor) + if target_floor < 0: + print(f"You are going down. Steps required: {steps}") + elif target_floor > 0: + print(f"You are going up. Steps required: {steps}") + else: + print(f"You are staying on the same floor. Steps required: {steps}") + except ValueError: + print("Invalid input. Please enter an integer.") + except AssertionError as e: print(f"Error: {e}") + + +if __name__ == "__main__": + import doctest + doctest.testmod() # Run tests on the core function + # Uncomment the following line for interactive user input + steps_to_floor() +