Dear list,
i have problem, i was going through with python programming and found there is something missing in case of float data types.
what i am trying to do is:
i am writing a simple program in which i asked user to input salary, i want to ristrict user to input anything irrelevant, for that i can use "try and exception", but how it will be done without using "try and exception",
like there something lke "isdigit, isalpha", by using this user cannot enter anything other than interger or alphanumeric, but in case of flot i have no clue.
here i am posting my code which i used for integer by using isdigit :

import sys
weight = raw_input("enter the weight: ")
while not (weight.isdigit()):     # check of weight is a digit, if not get valid entry
    weight = raw_input ("enter the weight of boy in integer: ")

print weight

waiting for your reply.

use weight.isdecimal()or weight.isdidgit()

this is not working,
what i want is when i ask user to input some weight then user can only enter integer or float values if user will try to enter anything other than integer or float then it will tell user that you enter something wrong please enter the right value again.
like in my previous code if you run that code you will find that you can only enter numeric values you are not allow to enter anyvalue other than integer.
i think now you got my point.

You could roll your own function, something like this ...

def isnumeric(s):
    '''returns True if s is numeric'''
    return all(c in "0123456789.+-" for c in s)

With today's challenged educational levels this may not pass the test.

A more fool proved version ...

''' isnumeric.py
test a numeric string s if it's usable for int(s) or float(s)
'''

def isnumeric(s):
    '''returns True if string s is numeric'''
    return all(c in "0123456789.+-" for c in s) and any(c in "0123456789" for c in s)

# test module ...
if __name__ == '__main__':
    print(isnumeric('123'))      # True
    print(isnumeric('-123.45'))  # True
    print(isnumeric('+3.14'))    # True
    print(isnumeric('$99.95'))   # False
    print(isnumeric('.'))        # False

Edited 3 Years Ago by vegaseat: fool

You could roll your own function, something like this ...

def isnumeric(s):
    '''returns True if s is numeric'''
    return all(c in "0123456789.+-" for c in s)

sorry sir,this wil not work if user only input ".", according to your code, your code will return true , if user only input a single or multiple dots(".....")

Can use isinstance.

>>> weight = float(raw_input("enter the weight: "))
enter the weight: 100.0
>>> weight
100.0

>>> type(weight)
<type 'float'>

>>> isinstance(weight, float)
True

>>> isinstance(weight, int)
False

>>> weight = 50
>>> isinstance(weight, float)
False

>>> #If you want both int and float to be True
>>> isinstance(weight, (int,float))
True

>>> a = 'hello'
>>> isinstance(a, (int,float))
False

As info.
In Python type checking is someting we try to avoid.
A couple lines you sure will hear more about if you continue to use Python.

If it looks like a duck, quacks like a duck - it's a duck.
it's Easier to Ask Forgiveness than Ask Permission.

Edited 3 Years Ago by snippsat

Can use isinstance.

>>> weight = float(raw_input("enter the weight: "))
enter the weight: 100.0
>>> weight
100.0
>>> type(weight)
<type 'float'>
>>> isinstance(weight, float)
True
>>> isinstance(weight, int)
False
>>> weight = 50
>>> isinstance(weight, float)
False

sir your suggestion is right , i tried it with try and except and it works fine, here is my code

b = 0
while b == False:
    try:
        a = float(raw_input("Give me a number"))
        b = isinstance(a, float)

    except ValueError:
        print "Could you at least give me an actual number?"

print "number is: ", a

but i am not getting how to use it without try and except.

If the user enters an integer, your program should be smart enough to use it as a float.
I also understood you didn't want to use "try except" error handling.

Quoting:

... but how it will be done without using "try and exception"

Edited 3 Years Ago by ZZucker

However, if you want to use the error handler, things are a lot simpler and an integer wil be changed to a float too:

while True:
    s = raw_input("Enter a float: ")
    try:
        n = float(s)
        break
    except ValueError:
        pass

print(n)

Edited 3 Years Ago by ZZucker

However, if you want to use the error handler, things are a lot simpler and an integer wil be changed to a float too:

Sir if i use try and except i can easily handel errors, and by using isdigit() and isalpha() i can perform the same task without using try and except , but for float things are still out of my range :(

One more option ...

# use Tkinter's simple dialogs (pop-up) to ask for input
# minvalue and maxvalue are optional

try:
    # Python2
    import Tkinter as tk
    import tkSimpleDialog as tksd
except ImportError:
    # Python3
    import tkinter as tk
    import tkinter.simpledialog as tksd

root = tk.Tk()
# show input dialogs without the Tkinter window
root.withdraw()

# if a non-numeric value is entered, will prompt again
# integer entries are converted to floats
price = tksd.askfloat("Price", "Enter the price of the item:")

print(price)
Comments
Nice!
This question has already been answered. Start a new discussion instead.