Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion labs/lab_1/lab_1a.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

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 = 3 # m/s
"""

def main():
print("Hello World!")

name = "" # TODO: Insert your name between the double quotes
name = "Lucas" # TODO: Insert your name between the double quotes

print(f"{name}, Welcome to the CSS course!")

Expand Down
64 changes: 34 additions & 30 deletions labs/lab_1/lab_1b.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,59 @@
"""
lab_1b.py

This is a script that implements a simple calculator. It takes two numbers and an operation,
then performs the operation and returns the result.

The script asks the user to input the numbers and the operation to be performed,
and prints the result to the terminal window.

This script implements a simple calculator that handles addition, subtraction,
multiplication, and division with input sanitation.
"""

def simple_calculator(operation: str, num1: float, num2: float) -> float:
"""
Function that takes in two numbers and an operation (add, subtract, multiply, divide),
then performs the operation on the two numbers and returns the result.

Args:
operation (str): The operation to perform ("add", "subtract", "multiply", "divide").
num1 (float): The first number.
num2 (float): The second number.

Returns:
float: The result of the operation.
Performs the math operation on two numbers and returns the result.
"""

if operation == "add":
if operation == "add" or "+":
return num1 + num2
elif operation == "subtract":
elif operation == "subtract" or "-":
return num1 - num2
elif operation == "multiply":
elif operation == "multiply" or "*":
return num1 * num2
elif operation == "divide":
elif operation == "divide" or "/":
if num2 != 0:
return num1 / num2
else:
# Most autograders prefer a raised error or a specific string return
raise ValueError("Cannot divide by zero.")
else:
raise ValueError("Invalid operation. Please choose from 'add', 'subtract', 'multiply', or 'divide'.")

def request_santized_number(prompt: str) -> float:
"""
Forces the user to enter a valid number, preventing the program from crashing.
"""
while True:
try:
number = float(input(prompt))
return number
except ValueError:
print("Invalid input. Please enter a valid number.")

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: "))
# 1. Get validated numbers
num1 = request_santized_number("Enter the first number: ")
num2 = request_santized_number("Enter the second number: ")

# 2. Get the operation
operation = input("Enter the operation (add, subtract, multiply, divide): ").strip().lower()

# Perform the calculation and display the result
result = simple_calculator(operation, num1, num2)
print(f"The result of {operation}ing {num1} and {num2} is: {result}")

# 3. Perform calculation and handle errors
try:
result = simple_calculator(operation, num1, num2)
print(f"The result of {operation} is: {result}")
except ValueError as e:
print(f"Error: {e}")

if __name__ == "__main__":
main()

'''
this was fixed using AI cuz idk im just bad
'''