-1

i made a simple calulator and im trying to catch an error with the try and except function but its not catching the error for some reason. The error is when i put anything other than a number in the num1 or num2 input. Im receiving a ValueError, but im putting ValueError in the except block and it is still not catching it. I want it to catch the error and print put invalid number instead of actually giving me an error in pycharm.

num1 = float(input("give a number"))
op = input("give an operator")
num2 = float(input("give a number"))


try:
    if op == "/":
     print(num1/num2)
    elif op == "*":
     print(num1*num2)
    elif op == "-":
     print(num1-num2)
    elif op == "+":
     print(num1+num2)
    else:
     if op != "/*-+":
         print("invalid operator")
except ValueError:
    print("invalid number")

this is the error that its giving me

give a number+
Traceback (most recent call last):
  File "C:/Users/Kalen/PycharmProjects/m/m.py", line 3, in <module>
    num1 = float(input("give a number"))
ValueError: could not convert string to float: '+'
caleb282
  • 11
  • 3
  • 1
    FWIW, `if op != "/*-+"` should be `if op not in "/*-+"` – DeepSpace Oct 01 '19 at 18:46
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – G. Anderson Oct 01 '19 at 18:49
  • You're taking the input and trying to convert it to float BEFORE you get to any of your validation block. You need to change the order – G. Anderson Oct 01 '19 at 18:50
  • When your program says "give a number" you gave it a plus sign, that plus sign isn't a number and cannot be converted to `float` – Ofer Sadan Oct 01 '19 at 19:09
  • what the difference if I put if op not in "/*-+" instead of if op != "/*-+"? and i copy and pasted the num1/op/num2 variables and put them inside the try and except block and now it works how i wanted it to, thank you G.Anderson – caleb282 Oct 01 '19 at 19:24

2 Answers2

0
num1 = input("give a number")
op = input("give an operator")
num2 = input("give a number")

try:
    num1=float(num1)
    num2=float(num2)
    if op == "/":
     print(num1/num2)
    elif op == "*":
     print(num1*num2)
    elif op == "-":
     print(num1-num2)
    elif op == "+":
     print(num1+num2)
    else:
     if op != "/*-+":
         print("invalid operator")
except ValueError:
    print("invalid number")

Try this hopefully it would work as expected.

Vashdev Heerani
  • 660
  • 7
  • 21
0

You are attempting to cast the inputs to floats before the catch block, so the error appears before the catch block and is not caught. Read the inputs outside of the catch block, but only cast them to floats later inside the catch block.

Even better, instead of trying to cast and throw ValueErrors, first check wether the string is actually a number. Do this with string.isdigit().

Validating input by throwing and catching exceptions is bad practice. The general advise is to check and validate all kinds of conditions that you can reasonably expect, and only catch and throw exceptions for the situations that you cannot foresee. When you write a try-catch clause, you are assuming that some error might happen for some reason out of your control but you don't know why, where or how. throwing exceptions is for errors, not just for normal foreseeable program flow.

if not (num1.isdigit() and num2.isdigit()):
      print("invalid number")
else:
      // proceed


Pieter Sap
  • 129
  • 2
  • 7