diff --git a/calculator.py b/calculator.py
index a46affd..ba8641f 100644
--- a/calculator.py
+++ b/calculator.py
@@ -40,45 +40,91 @@
"""
-
-
+def instructions():
+ """ Returns and instructional page at root """
+ page = """
+
How to use this page
+ Instructions
+ For the following cases, replace the numbers 3 & 4 with the numbers you wish to use in the search box.
+ The first number is acted on by the second. e.g. divide/3/4 would be 3 divided by 4.
+
+ - For addition use: add/3/4
+ - For subtraction use: subtract/3/4
+ - For multiplication use: multiply/3/4
+ - For division use: divide/3/4
+
+ """
+ return page
+
+
+ # return str([])
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"
+ sum_val = str(sum(map(int, args)))
+ return sum_val
+
+def subtract(*args):
+ """ Returns a STRING with the difference of the arguments """
+ return str(int(args[0]) - int(args[1]))
+
- return sum
+def multiply(*args):
+ """ Returns a STRING with the product of the arguments """
+ return str(int(args[0])*int(args[1]))
-# TODO: Add functions for handling more arithmetic operations.
+def divide(*args):
+ """ Returns a STRING with the quotient of the arguments """
+ return str(int(args[0]) / int(args[1]))
def resolve_path(path):
"""
Should return two values: a callable and an iterable of
arguments.
"""
+ funcs = {
+ '': instructions,
+ 'add': add,
+ 'subtract': subtract,
+ 'multiply': multiply,
+ 'divide': divide
+ }
+
+ path = path.strip('/').split('/')
+
+ func_name = path[0]
+ args = path[1:]
- # TODO: Provide correct values for func and args. The
- # examples provide the correct *syntax*, but you should
- # determine the actual values of func and args using the
- # path.
- func = add
- args = ['25', '32']
+ try:
+ func = funcs[func_name]
+ except KeyError:
+ raise NameError
return func, args
def application(environ, start_response):
- # TODO: Your application code from the book database
- # work here as well! Remember that your application must
- # invoke start_response(status, headers) and also return
- # the body of the response in BYTE encoding.
- #
- # TODO (bonus): Add error handling for a user attempting
- # to divide by zero.
- pass
+ headers = [("Content-type", "text/html")]
+ try:
+ path = environ.get("PATH_INFO", None)
+ if path is None:
+ raise NameError
+ func, args = resolve_path(path)
+ body = func(*args)
+ status = "200 OK"
+ except NameError:
+ status = "404 Not Found"
+ body = "Not Found
"
+ except ZeroDivisionError:
+ status = "400 Bad Request"
+ body = "Divide by zero not allowed
"
+ finally:
+ headers.append(('Content-length', str(len(body))))
+ start_response(status, headers)
+ return [body.encode('utf8')]
if __name__ == '__main__':
- # TODO: Insert the same boilerplate wsgiref simple
- # server creation that you used in the book database.
- pass
+ from wsgiref.simple_server import make_server
+ srv = make_server('localhost', 8080, application)
+ srv.serve_forever()