## Creating a calculator with condition

# 1
Creating a calculator with condition

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

Create a calculator application in your home folder called itncacl that will perform the following applications:

Subtract two numbers
Multiply two numbers
Divide two numbers
You cannot divide by zero, so create a condition that checks for and prevents a that condition.

2. Relevant commands, code, scripts, algorithms:

3. The attempts at a solution (include all code and scripts):
My only question is how do I add to my code so that when I try to divide by zero it will show Cannot divide by zero or invalid? Thanks!

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Northern Virginia Comm College, VA, USA, Saleh, ITN171

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by Scott; 11-20-2017 at 05:07 PM.. Reason: Please use code tags
 mugiboya View Public Profile for mugiboya Find all posts by mugiboya
# 2
I would think the simplest way to handle this would be to add an additional condition inside your pre-existing condition (that is not related to Obama Care ) to test if "n2" is 0 (zero) before you attempt to divide by it:

e.g.
If the intention is not to show that you are dividing by zero, but to prevent it doing so altogether, you can amend the existing elif that handles that case to also check that "n2" is not zero. Talking of which, it may be better (simpler) to use a "case" statement instead of "if" - ifs can get a bit clumsy looking, when there's lots of elifs going on.
This User Gave Thanks to Scott For This Post:
 Scott View Public Profile for Scott Find all posts by Scott
# 3
 mugiboya View Public Profile for mugiboya Find all posts by mugiboya
# 4
Scotts solution has the disadvantage that, if someone would enter 00 as the second number, you would still get a division by zero.

In general, I prefer testing numerical values in numerical context, for instance

This User Gave Thanks to rovf For This Post:
 rovf View Public Profile for rovf Find all posts by rovf
# 5
How about SED commands? Will they work for such a task? I should extract records based on a particular condition in my project.
 florachavezz View Public Profile for florachavezz Find all posts by florachavezz
# 6
Quote:
Originally Posted by florachavezz
How about SED commands? Will they work for such a task? I should extract records based on a particular condition in my project.
Sorry, I don't quite understand the point here. In your posting, you wrote: My only question is how do I add to my code so that when I try to divide by zero it will show Cannot divide by zero or invalid? I don't see how this relates to record extraction.