0
votes

So I need to make a calculator that converts strings into floats then calculate. The problem is I need to make error messages whenever:

  1. the user enters a string that does not contain operands and/or an operator.
  2. the user does not enter anything (the user simply pressed the Enter key).
  3. the user only enters an operand.
  4. the user only enters an operator.
  5. the user enters two operands.
  6. the user enters an operand and an operator.
  7. the user is trying to divide a number by 0.
  8. the user did not put a space in between the operands and the operator.

This is how the code looks like without error messages

# Interface
print ("Equation Calculator")
print (" ")
print ("My Equation Calculator is able to")
print ("     Add: +")
print ("     Subtract: -")
print ("     Multiply: *")
print ("     Divide: /")
print (" ")
print ("The equation you enter must follow this syntax:")
print (" <openrand><speace><operator><space><operand>.")
print ("An <operand> is any float number.")
print ("An <operator> is any is any of the operators mentioned above.")
print ("A <space> is an empty space.")

#  Enter the equation
equation = input ("Enter your equation: ")

# Split the equation into Operand 1,2 and Operator
operand1,operator,operand2 = equation.split(" ")


# Show the user the equation
print ("Here is the equation you have entered: " + equation)

#  Addition, Converting strings (operand 1 and 2) into float
if (operator == "+"):
    answer = float(operand1) + float(operand2)

#  Subtraction, Converting strings (operand 1 and 2) into float
if (operator == "-"):
    answer = float(operand1) - float(operand2)

#  Multiplication, Converting strings (operand 1 and 2) into float
if (operator == "*"):
    answer = float(operand1) * float(operand2)

#  DIvision, Converting strings (operand 1 and 2) into float
if (operator == "/"):
    answer = float(operand1) / float(operand2)

# Display the answer
print ("The answer is: ",answer )

So for 7 and 8th errors I did this

# Error for not having a space
if (equation.find(" ") == False):
    print ("Error #1: Please check if there is a space in between the two operands and the operator.")

# Error for dividing by 0
if (operand2 == "0"):
    print ("Error #7: You cannot divide by 0.")

However python just bypasses this and still crashes. What is the problem with above code? How can I make it so the code prints error messages in above 8 situations? Also I cannot use the built-in functions eval( ) or exec(), break or continue or pass or sys.exit( ). I am very new to programming in general. Please help and thank you.

3
use else statements - Julien
You could do try: operand1,operator,operand2 = equation.split("") except ValueError: to make sure you get 3 strings in equation. You can do a similar thing to catch ValueError to make sure the operand strings can be converted to floats: try: operand1=float(operand1) except ValueError:. See Exceptions in the tutorial for details. - PM 2Ring

3 Answers

1
votes

If you have some set requirements on the incoming data, it is usually a good idea to check beforehand, if the data match this requirements. So I would test if the string given matches your requirements: The general structure could be tested with regular expressions:

I assume only integers are used, if not you need to replace the \ds with floating pointg matching, which should be googleable.

\d+ [+\/\-*] \d+

This regex matches any digit, followed by a space,followed by one of the operator +/*- and a digit.

Please not that would cause a very generic error message - like "your input does not look like the required input", so you might be better of to test the parts separately, to give a better user experience.

0
votes

Use a structure like this:

if bad_condition:
    print "error"
else:
    # do whatever
0
votes

You can try this:

try:
    assert condition, "Error Message"
except AssertionError, e:
    raise Exception(e.args)

for example:

# Error for not having a space
try:
    assert (equation.find(" ")), "Error #1: Please check if there is a space in between the two operands and the operator."
except AssertionError, e:
    raise Exception(e.args)