diff --git a/README.md b/README.md index 3a87181..b0877e6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Practice with git and GitHub -\[Your Step 3 Edit Here\] +I'm Javier de la Rosa and I edited this file! This is a very simple repository for practicing with git and GitHub. git is a utility for *version control*. When a body of code is tracked with git, it is easy to see how the software has evolved over time, to roll back changes when needed, and to incorporate modifications by multiple collaborators. In this activity, we're going to focus on core git workflows for single-person projects. We may do a follow-up activity later in the quarter on workflows for collaborative projects. @@ -109,4 +109,4 @@ An important principle of version control is that you **never** duplicate files. Great job! If you comfortably navigated these exercises, then you have the necessary basics for working with git and GitHub. These will get you most of the way through the course, and indeed, through your programming career. The most important topics that we haven't yet discussed are *merging* and *branching*, which are especially relevant when collaborating with others. We may come back to these in a future Discussion activity. -Another topic that you might find useful to explore on your own is the `.gitignore` file. This file specifies files which should be *excluded* from tracking by git. This is handy if there are certain "junk" files that you would prefer not to see in GitHub Desktop. +Another topic that you might find useful to explore on your own is the `.gitignore` file. This file specifies files which should be *excluded* from tracking by git. This is handy if there are certain "junk" files that you would prefer not to see in GitHub Desktop. \ No newline at end of file diff --git a/calculator.py b/calculator.py new file mode 100644 index 0000000..ab09ba3 --- /dev/null +++ b/calculator.py @@ -0,0 +1,99 @@ +# calculator.py +# A minimal Flask web app that presents a form to enter two numbers and select an arithmetic operation. +# How to run: +# 1) Ensure Flask is installed: pip install flask +# 2) Start the app: python calculator.py +# 3) Open your browser at: http://127.0.0.1:5000/ + +from __future__ import annotations +from flask import Flask, request, render_template_string +from typing import Optional + +app = Flask(__name__) + +PAGE_TEMPLATE = """ + + + + + + Simple Calculator + + + +
+

Simple Calculator

+

Enter two numbers and choose an operation.

+ +
\n \n \n\n \n \n\n \n
\n \n \n \n \n
\n\n \n
\n\n {% if error %}\n
Error: {{ error }}
\n {% elif result is not none %}\n
Result: {{ result }}
\n {% endif %}\n
\n\n\n""" + + +def compute(a: float, b: float, op: str) -> float: + """Compute the operation on a and b. + + Args: + a: First number + b: Second number + op: One of 'plus', 'minus', 'times', 'divide' + Returns: + The computed float result. + Raises: + ValueError: If op is invalid or division by zero occurs. + """ + if op == "plus": + return a + b + elif op == "minus": + return a - b + elif op == "times": + return a * b + elif op == "divide": + if b == 0: + raise ValueError("Cannot divide by zero.") + return a / b + else: + raise ValueError("Invalid operation.") + + +@app.route("/", methods=["GET"]) +def index(): + # Initial page load with empty fields + return render_template_string(PAGE_TEMPLATE, a=None, b=None, op="plus", result=None, error=None) + + +@app.route("/calculate", methods=["POST"]) +def calculate(): + a_raw: Optional[str] = request.form.get("a") + b_raw: Optional[str] = request.form.get("b") + op: str = request.form.get("op", "plus") + + error: Optional[str] = None + result: Optional[float] = None + + try: + if a_raw is None or b_raw is None: + raise ValueError("Both numbers are required.") + a = float(a_raw) + b = float(b_raw) + result = compute(a, b, op) + except ValueError as e: + error = str(e) + + # Re-render page with the result or error and preserve inputs + return render_template_string(PAGE_TEMPLATE, a=a_raw, b=b_raw, op=op, result=result, error=error) + + +if __name__ == "__main__": + # Run the Flask development server + app.run(debug=True)