Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator Help..?
#1
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
Reply
#2
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
Reply
#3
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.
Reply
#4
Looking at other designs, it looks as though the answer is to return infinity
see: https://www.daniweb.com/programming/soft...exceptions
Reply
#5
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!
Reply
#6
(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))
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020