Python Forum
Calculator Help..? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Calculator Help..? (/thread-2940.html)



Calculator Help..? - hentera - Apr-20-2017

Hello, I'm so stuck with my assignment. So far I have built the program, I just need help with dividing by the zero exception? What is that exactly I keep getting mixed results from different websites. And how do I implement the sys.exit(function) into my program?

1) The sys.exit( ) function can be used to exit the program from any code location. The sys module (a python library) can be imported into your program with an import statement at the top of your code file.

2) If the user chose division, use exception handling to detect a possible divide by zero exception. If an exception occurs, print out an appropriate error message and exit the program gracefully.


print("Select An Operator")
print("Addition:        +")
print("Subtraction:     -")
print("Multiplication:  *")
print("Division:        /")

operator = input("Enter Choice (+,-,*,/:) ")

number = int(input("Please enter a number: "))
number2 = int(input("Please enter a second number: "))

if operator == '+':
    print(number + number2)
elif operator == '-':
    print(number - number2)
elif operator == '*':
    print(number * number2)
elif operator == '/':
    print(number / number2)
else:
    error



RE: Calculator Help..? - Kebap - Apr-20-2017

I assume you have (or should have) learned all that is needed to fulfill this task. In fact, most is already written in its description there. I can see no attempt to solve both points you wrote. Please show your attempts so we can advise


RE: Calculator Help..? - nilamo - Apr-20-2017

What is it? It's an error you get, if you try to do something that's impossible (or doesn't make sense, or returns Infinity or something. I don't know, I'm bad at math).

>>> 32 / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
What can you do about it? try/except, and display a nicer error message than simply crashing.


RE: Calculator Help..? - Larz60+ - Apr-20-2017

Looking at other designs, it looks as though the answer is to return infinity
see: https://www.daniweb.com/programming/software-development/threads/262372/calculator-exceptions


RE: Calculator Help..? - Josh - Apr-20-2017

You can use an exception to achieve this.

With the code that you have used, I would do this.


print("Select An Operator")
print("Addition:        +")
print("Subtraction:     -")
print("Multiplication:  *")
print("Division:        /")

operator = input("Enter Choice (+,-,*,/:) ")

number = int(input("Please enter a number: "))
number2 = int(input("Please enter a second number: "))

try:
 if operator == '+':
     print(number + number2)
 elif operator == '-':
     print(number - number2)
 elif operator == '*':
     print(number * number2)
 elif operator == '/':
     print(number / number2)
 else: print("ERROR!")
except: print("ERROR!")
This code will "try" to do the task, and will output "ERROR" if an error is returned as we have used an exception.

If I was writing a calculator, I would just write something like this:


try:print(eval(input("Enter the equation")))
except:print("Error")
It takes input like 5*3, evaluates it and outputs the result. If there is an error in the process, "Error" will be outputted.

I hope this helps!


RE: Calculator Help..? - nilamo - Apr-20-2017

(Apr-20-2017, 06:13 PM)Josh Wrote: If I was writing a calculator, I would just write something like this:

try:print(eval(input("Enter the equation")))
except:print("Error")

*vomit*
Please don't suggest eval.

I'd do something like...
import operator as ops

operations = {
    "+": ops.add,
    "-": ops.sub,
    "x": ops.mul,
    "*": ops.mul,
    "/": ops.truediv,
}

op = operations[input("What do you want to do? [{0}] ".format(', '.join(operations.keys())))]
left = float(input("First number: "))
right = float(input("Second number: "))

print(op(left, right))