0

I'm new to coding and learning python... I was trying to check if an input is a number or not. I found a great answer from an inactive account 3 years ago that looks like this:

a=(raw_input("Amount:"))

try:
    int(a)
except ValueError:
    try:
        float(a)
    except ValueError:
        print "This is not a number"
        a=0
if a==0:
    a=0
else:
    print a
    #Do stuff

https://stackoverflow.com/a/26451234/8032074

My question is, what exactly is happening from if a==0 until the end? I can tell that if I take it out, ALL input will end up getting printed, even if it's not a number.

But how exactly is that code preventing non-numerical entries from getting printed?

Thanks!!

Community
  • 1
  • 1
ebnhawk
  • 3
  • 1

4 Answers4

2

It works because a=0 set a to 0 if it's neither a float nor an int. after that, it checks if a == 0 a equal to 0, if it's not, else, it will print the input. A better version using the try...except...else syntax:

a=raw_input("Amount:")

try:
    float(a)
except ValueError:
    print "This is not a number"
else:
    print a
    #Do stuff

Here's a fun version:)

import re
a = raw_input("Amount:")
if re.match("-?\d*[\.|\d]\d*", a):
    print a
else:
    print "This is not a number"
Taku
  • 31,927
  • 11
  • 74
  • 85
1

The point of the last if statement is to make sure not to print anything out if the input is not a number.

If the input is not a number, the try/except makes sure the input is set to 0. Then, if the input is 0 (if the input was originally not a number), it is not printed out.

However, in the case the the value inputted was actually 0, I would suggest changing the code to the following:

a=(raw_input("Amount:"))

try:
    int(a)
except ValueError:
    try:
        float(a)
    except ValueError:
        print "This is not a number"
        a=None

if a is not None:
    print a
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0

The '==' operator tests if the object pointed to by the variable is the same. In this case, '0' is the constant. If you re-write it slightly it makes more sense:

a=(raw_input("Amount:"))

not_a_number = 0;

try:
    int(a)
except ValueError:
    try:
        float(a)
    except ValueError:
        print "This is not a number"
        a=not_a_number
if a==not_a_number:
    a=0
else:
    print a
    #Do stuff
Chris Rouffer
  • 743
  • 5
  • 14
  • 1
    This only works if we assume that for the rest of the code the invalid input will be treated the same as zero (which makes sense if it's an amount). Otherwise one should probably do `not_a_number = "invalid input"`. – Joooeey May 18 '17 at 15:54
0

In case anyone wonders, I ended up synthesizing what I learned and did this:

answer = (raw_input("gimme a number: "))

def is_numeric(number):
    try:
        int(number)
        return True
    except ValueError:
        try:
            float(number)
            return True
        except ValueError:
            print "that ain't a number"
            return False


def reply(number):
    if is_numeric(number):
        print "%s is a good number" % number

reply(answer)

(I'm practicing functions right now.) Thanks for helping me understand what I was looking at!

ebnhawk
  • 3
  • 1