Skip to content
Open
32 changes: 32 additions & 0 deletions .github/workflows/run_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: simple_calculator unit test

on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with Ruff
run: |
pip install ruff
ruff --format=github --target-version=py310 .
continue-on-error: true
- name: Test with pytest
run: |
coverage run -m pytest tests/tests_1b.py -v -s
- name: Generate Coverage Report
run: |
coverage report -m
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,14 +3,16 @@

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

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

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

print(f"{name}, Welcome to the CSS course!")
print("Hello all! My name is Om. I like STEM. My favorite subjects outside and inside of school are calculus, chemistry, computer science (particularly machine learning). I program competitively. I also cook food. I like to camp and enjoy the outdoors with my Scout troop.")

if __name__ == "__main__":
main()
21 changes: 19 additions & 2 deletions labs/lab_1/lab_1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,31 @@ def simple_calculator(operation: str, num1: float, num2: float) -> float:
raise ValueError("Cannot divide by zero.")
else:
raise ValueError("Invalid operation. Please choose from 'add', 'subtract', 'multiply', or 'divide'.")

def input_number(prompt: str) -> float:
"""
Helper function to request a number from the user and ensure it's a valid float.

Args:
prompt (str): The prompt to display to the user.

Returns:
float: The sanitized number input by the user.
"""
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: "))
num1 = input_number("Enter the first number: ")
num2 = input_number("Enter the second number: ")
operation = input("Enter the operation (add, subtract, multiply, divide): ").strip().lower()

# Perform the calculation and display the result
Expand Down
2 changes: 2 additions & 0 deletions tests/tests_1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ def test_addition():
assert simple_calculator("add", 5, 3) == 8 # Test for positive numbers
assert simple_calculator("add", -2, 2) == 0 # Test for negative and positive number
assert simple_calculator("add", 0, 0) == 0 # Test for zero addition
assert simple_calculator("add", -2, -4) == -6 # Test for negative numbers

def test_subtraction():
assert simple_calculator("subtract", 5, 3) == 2 # Test for positive numbers
assert simple_calculator("subtract", -2, -2) == 0 # Test for negative numbers
assert simple_calculator("subtract", 0, 5) == -5 # Test for zero minuend
assert simple_calculator("subtract", 0, -5) == 5

def test_multiplication():
assert simple_calculator("multiply", 5, 3) == 15 # Test for positive numbers
Expand Down