diff --git a/labs/lab_1/lab_1a.py b/labs/lab_1/lab_1a.py index 9d15ec83..42441319 100644 --- a/labs/lab_1/lab_1a.py +++ b/labs/lab_1/lab_1a.py @@ -1,16 +1,19 @@ -""" +"""" lab_1a.py The first lab in the BWSI CSS course. To complete this lab, fill out the variable on line 10 with your name. Then, save the code, add it to the staging area, and commit it to the Git tree. +This is to simulate a change made on a robot: robot_speed = 8 # m/s + """ def main(): print("Hello World!") - name = "" # TODO: Insert your name between the double quotes + name = "Sylvie Yao" print(f"{name}, Welcome to the CSS course!") + print("My name is Sylvie. I am junior from New Jersey. I like to play tennis and build puzzles!") if __name__ == "__main__": main() diff --git a/labs/lab_1/lab_1b.py b/labs/lab_1/lab_1b.py index e58dd957..3ff3f8e6 100644 --- a/labs/lab_1/lab_1b.py +++ b/labs/lab_1/lab_1b.py @@ -37,14 +37,44 @@ def simple_calculator(operation: str, num1: float, num2: float) -> float: else: raise ValueError("Invalid operation. Please choose from 'add', 'subtract', 'multiply', or 'divide'.") +def requested_sanitized_number(prompt: str) -> float: + """ + Function to request and sanitizie user input for the operation + + Returns: + float: The santized numeric input by the user + + """ + while True: + try: + number = float(input(prompt)) + return number + except ValueError: + print("Invalid input. Please enter a valid number.") + +def requested_sanitized_operation(prompt: str) -> str: + """ + Function to request and sanitize user input for a wrong operation + + Returns: + str: the santinized string input by the user + + """ + while True: + operation = input(prompt).strip().lower() + if (operation == "multiply") or (operation == "add") or (operation == "subtract") or (operation == "divide"): + return operation + else: + print("Invalid input. Please enter a valid operation.") + def main(): print(f"===== Simple Calculator =====") # Ask the user for sample input - num1 = float(input("Enter the first number: ")) - num2 = float(input("Enter the second number: ")) - operation = input("Enter the operation (add, subtract, multiply, divide): ").strip().lower() + num1 = requested_sanitized_number("Enter the first number: ") + num2 = requested_sanitized_number("Enter the secondabc number: ") + operation = requested_sanitized_operation("Enter the operation (add, subtract, multiply, divide): ") # Perform the calculation and display the result result = simple_calculator(operation, num1, num2)