Skip to content

My solution for the INE Introduction to Programming with Python project: Simple Calculator #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ Your job is to implement the code under `calculator.py`; note that we've left on

### Step 1:

* Add
* Subtract
* Multiply
- Add
- Subtract
- Multiply

We've set up 3 functions here that you need to finish. These are fairly straight-forward and shouldn't give you too much trouble, think back to your lesson on operators!

##### To run tests, use these commands in the command line:

```bash
Expand All @@ -26,9 +27,9 @@ $ py.test test_multiply.py

### Step 2

* Divide
- Divide

You'll notice here that divide is the only function that has two tests for it - that's because you'll need to program some exceptional behavior in case the user tries to divide by 0!
You'll notice here that divide is the only function that has two tests for it - that's because you'll need to program some exceptional behavior in case the user tries to divide by 0!

If 0 is passed as a denominator when using this function, instead of an integer, return a string warning the user not to divide by 0. You can chose any message you want, for example `return "Invalid value for denominator, cant't divide by 0!"`

Expand All @@ -38,14 +39,14 @@ $ py.test test_divide.py

### Step 3

* Square
* Power
* Square Root
- Square
- Power
- Square Root

Rounding out the last three tests are functions dealing with powers. They can be kind of tricky (especially finding the square root!) but you shouldn't have any problem if you review your lesson on operators.

```bash
$ py.test test_square.py
$ py.test test_power.py
$ py.test test_sqrt.py
$ py.test tests_sqrt.py
```
17 changes: 11 additions & 6 deletions calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ def add(x, y):


def subtract(x, y):
pass
return x - y


def divide(x, y):
pass
if y != 0:
return x / y
else:
return "Please ensure the denominator is a non-zero integer, as division by zero is not allowed."



def multiply(x, y):
pass
return x * y


def square(x):
pass
return x * x



def power(x, y):
pass
return x ** y


def sqrt(x):
pass
return x ** 0.5