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!!

5
  • 1
    Have you tried entering 0 as your input (which is numeric)? Commented May 18, 2017 at 15:40
  • 6
    What you have found is not a great answer at all. That if a==0: a=0 in particular looks like it was written by someone with some confusion. Commented May 18, 2017 at 15:43
  • remove a=0 from exception. it changes value of your variable Commented May 18, 2017 at 15:43
  • It works with 0, with and without the code at the bottom. I did find a lot of answers, but this is the only one that actually worked in 2.7... seems like this is easier to do in 3. Commented May 18, 2017 at 15:48
  • Please clarify if you want a float (can have decimals) or an int (no decimals allowed). Commented May 18, 2017 at 16:05

4 Answers 4

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"
Sign up to request clarification or add additional context in comments.

4 Comments

Still not sure what the point is of casting to int and to float .
If the input is 1.1 (float), int(a) will raise an exception, while float(a) will not @khelwood
So why bother with int if you're going to fall back on trying float anyway?
If the user input is 0 it will get printed, so entering an amount of 0 will work. User input isn't an int, it's a string.
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

Comments

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

1 Comment

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".
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.