diff --git a/labs/lab_1/lab_1a.py b/labs/lab_1/lab_1a.py index 9d15ec83..82528f91 100644 --- a/labs/lab_1/lab_1a.py +++ b/labs/lab_1/lab_1a.py @@ -3,12 +3,13 @@ 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 = "Eric Xu" # TODO: Insert your name between the double quotes print(f"{name}, Welcome to the CSS course!") diff --git a/labs/lab_1/lab_1b.py b/labs/lab_1/lab_1b.py index e58dd957..d5ebe3df 100644 --- a/labs/lab_1/lab_1b.py +++ b/labs/lab_1/lab_1b.py @@ -8,7 +8,24 @@ and prints the result to the terminal window. """ +def request_sanatized_num(prompt: str) -> float: + while True: + try: + number = float(input(prompt)) + return number + except ValueError: + print("Invalid input number. Please enter a valid number.") +def request_sanitized_operation(prompt: str) -> str: + valid_operations = {"add", "subtract", "multiply", "divide"} + + while True: + operation = input(prompt).strip().lower() + if operation in valid_operations: + return operation + else: + print("Invalid operation. Please choose from add, subtract, multiply, or divide.") + def simple_calculator(operation: str, num1: float, num2: float) -> float: """ Function that takes in two numbers and an operation (add, subtract, multiply, divide), @@ -42,8 +59,8 @@ 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: ")) + num1 = request_sanatized_num("Enter the first number: ") + num2 = request_sanatized_num("Enter the second number: ") operation = input("Enter the operation (add, subtract, multiply, divide): ").strip().lower() # Perform the calculation and display the result