diff --git a/README.md b/README.md index 8bc427c..2165854 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,15 @@ Your assignment is to create a WSGI calculator that you can use to add, subtract You'll use the calculator by visiting an address like: [http://localhost:8080/multiply/3/5](http://localhost:8080/multiply/3/5). Once you've completed the assignment, if you were to run the program and visit this page then you would expect to see a page in your browser with the text "15". -You'll also have to create one more page: an index page at the address http://localhost:8080/ with some text instructions that explain (in just a few sentences) how to use the site. +You'll also have to create one more page: an index page at the address [http://localhost:8080/](http://localhost:8080/) with some text instructions that explain (in just a few sentences) how to use the site. ## How to Know When You're Done When you have completed the TODOs, you should be able to visit the following pages and see a page with the indicated content.: - * http://localhost:8080/multiply/3/5 => 15 - * http://localhost:8080/add/23/42 => 65 - * http://localhost:8080/subtract/23/42 => -19 - * http://localhost:8080/divide/22/11 => 2 - * http://localhost:8080/ => Here's how to use this page... (etc.) + * [httpL//localhost:8080/add/3/5](http://localhost:8080/multiply/3/5) => 15 + * [http://localhost:8080/add/23/42](http://localhost:8080/add/23/42) => 65 + * [http://localhost:8080/subtract/23/42](http://localhost:8080/subtract/23/42) => -19 + * [http://localhost:8080/divide/22/11](http://localhost:8080/divide/22/11) => 2 + * [http://localhost:8080/](http://localhost:8080/) => Here's how to use this page... (etc.) There is also a set of tests for you to run, using `python tests.py`. diff --git a/calculator.py b/calculator.py index a46affd..9cdab27 100644 --- a/calculator.py +++ b/calculator.py @@ -1,3 +1,6 @@ +import re +import traceback + """ For your homework this week, you'll be creating a wsgi application of your own. @@ -44,14 +47,50 @@ def add(*args): """ Returns a STRING with the sum of the arguments """ - # TODO: Fill sum with the correct value, based on the # args provided. - sum = "0" - - return sum - -# TODO: Add functions for handling more arithmetic operations. + try: + res = str(int(args[0]) + int(args[1])) + return '{} + {} = {}'.format(args[0], args[1], res) + except ValueError: + raise ValueError + +def substract(*args): + """ Returns a STRING with the subtract of the arguments """ + try: + res = str(int(args[0]) - int(args[1])) + return '{} - {} = {}'.format(args[0], args[1], res) + except ValueError: + raise ValueError + +def multiply(*args): + """ Returns a STRING with the multiply of the arguments """ + try: + res = str(int(args[0]) * int(args[1])) + return '{} * {} = {}'.format(args[0], args[1], res) + except ValueError: + raise ValueError + +def divide(*args): + """ Returns a STRING with the divide of the arguments """ + try: + res = str(int(args[0]) / int(args[1])) + return '{} / {} = {}'.format(args[0], args[1], res) + except ValueError: + if args[1] == 0: + raise ZeroDivisionError + raise ValueError + +def instructions(): + """ Returns a STRING about how to use the calculator """ + ins = """ +