-2
print "HEllo !!"
print "Today, I will calculate something for you !!"#introduces itself 
inp = True
def start():
    print "type 1 for ADDITION"
    print "type 2 for SUBTRACTION"
    print "type 3 for MULTIPLICATION"      #asks for the kind of calculation 
    print "type 4 for DIVISION"
    press=int(raw_input())
    if press >= 5 :
            print "Choose a NUMBER which is less than 5 and you will get the answer!!"
            print "-------------------------------"
    if press == 1:
        print "Enter your first number"
        num1 = input()
        print "enter your second number"            #adds 
        num2 = input()
        result = num1 + num2
        print " Your answer is "+ str(result)
        print "-------------------------------"
    if press == 2 :
        print "Enter your first number"
        num1 = input()
        print "enter your second number"            #subtracts      
        num2 = input()
        result = num1 - num2
        print " Your answer is "+ str(result)
        print "-------------------------------"
    if press == 3:
        print "Enter your first number"
        num1 = input()
        print "enter your second number"
        num2 = input()                              #multiplies             
        result = num1 * num2
        print " Your answer is "+ str(result)
        print "-------------------------------"
    if press == 4:
        print "Enter your first number"
        num1 = input()                              #divides 
        print "enter your second number"
        num2 = input()
        result = num1/num2
        print " Your answer is "+ str(result)
        print "-------------------------------"
while inp : 
    start()

So I've created this calculator using basic commands, and the only problem is when someone types in a letter, where it asks for a number from 1 to 4, it shows a python error. Instead I want to be able to print a custom made error like "only number allowed" or something like that instead of the regular error that python shows. What change can I do so that it takes in numbers and alphabets, but shows a custom-made error when someone enters a letter ? P.S. : I am asking for the var "press" and not about "num1" or "num2"

  • P.S. : I am asking for the var "press" and not about "num1" or "num2" and it's not a duplicate. The similar question is way too different from what I'm asking. Please see again and reopen this. –  Jun 29 '16 at 20:27

2 Answers2

3

You could do:

num1 = None
while num1 is None:
    try:
        num1 = int(num1)
    except ValueError:
        print "ERROR: Invalid Input"        

This would attempt to cast the input to an integer. If it failed to do so, then the code in the "except" part will run.

Jordan Bonitatis
  • 1,527
  • 14
  • 12
mrwyatt
  • 183
  • 6
  • No, that won't work because by the time you'd be able to check that, the exception will have been thrown already. Also, naked excepts are super dangerous. You should be using the strictest except possible, `ValueError` in this case. – Morgan Thrapp Jun 29 '16 at 20:24
  • I am asking for the var "press" and not about "num1" or "num2" and it's not a duplicate. –  Jun 29 '16 at 20:31
  • The same logic can be applied to the press variable. – mrwyatt Jun 29 '16 at 20:34
  • 1
    This isn't a big deal because this term gets thrown around loosely, but my understanding is that `int(num1)` isn't a cast, it is a function that constructs an int from other numbers or strings. You don't really cast things in Python. – juanpa.arrivillaga Jun 29 '16 at 20:38
0

You can use str.isdigit() to check if the string only contains numbers, it returns true if that is the case and false if the string contains any non number characters.

iciaran
  • 41
  • 5