0

When I type in 23.3 and 44 it outputs only one of these values is a valid number And a Valid number basically is any number that has no strings how can I fix this where 23.3 and 44 outputs both are values

x = input(":")
y = input(":")
if x.isnumeric() and y.isnumeric():
    print('both are valid numbers')
elif  x.isnumeric() or y.isnumeric():
    print('one of the values  is a valid number ')
else: print('none are valid numbers')
  • Does this answer your question? [How can I check if a string represents an int, without using try/except?](https://stackoverflow.com/questions/1265665/how-can-i-check-if-a-string-represents-an-int-without-using-try-except) – Passerby Oct 21 '21 at 03:37
  • above will not address float but this might be an answer: [How to check if a raw input is an integer or float (including negatives) without using try-except](https://stackoverflow.com/questions/57543550/how-to-check-if-a-raw-input-is-an-integer-or-float-including-negatives-without) – Neea Oct 21 '21 at 03:42
  • 1
    Does this answer your question? [How to check if a raw input is an integer or float (including negatives) without using try-except](https://stackoverflow.com/questions/57543550/how-to-check-if-a-raw-input-is-an-integer-or-float-including-negatives-without) – Brian61354270 Oct 21 '21 at 03:47

4 Answers4

0

The isnumeric method does not return True for the decimal separator.

Passerby
  • 808
  • 1
  • 5
  • 9
0

isnumeric() only checks whether the value is an integer, but not if it is a float.

Unfortunately, I haven't found a very clean way to handle this. I think the best solution is to implement your own function, maybe something like this:

def is_decimal(s):
    try:
        float(s)
    except ValueError:
        return False
    else:
        return True

x = input(":")
y = input(":")
if is_decimal(x) and is_decimal(y):
    print('both are valid numbers')
elif is_decimal(x) or is_decimal(y):
    print('one of the values is a valid number ')
else: print('none are valid numbers')
Kyle Dixon
  • 474
  • 2
  • 10
0
def is_number(s):
if s.count(".")==1:   
    if s[0]=="-":
        s=s[1:]
    if s[0]==".":
        return False
    s=s.replace(".","")
    for i in s:
        if i not in "0123456789":
            return False
    else:                
        return True
elif s.count(".")==0:  
    if s[0]=="-":
        s=s[1:]
    for i in s:
        if i not in "0123456789":
            return False
    else:
        return True
else:
    return False

x = input(":") y = input(":") if is_number(x) and is_number(y): print('both are valid numbers') elif is_number(x) or is_number(y): print('one of the values is a valid number ') else: print('none are valid numbers')

-1

This is the description of str.isnumeric in the documentation.

Return True if there is at least one character in the string and all characters are numeric characters, False otherwise.

So it can't determine float directly.

You can use a regular expression to determine whether it is a float.

re

import re

regex = re.compile(r"^\d+(?:\.\d+)?$")

x = input(":")
y = input(":")


def is_numeric(s: str):
    return s.isnumeric() or regex.match(s)


x_is_numeric = is_numeric(x)
y_is_numeric = is_numeric(y)
if x_is_numeric and y_is_numeric:
    print('both are valid numbers')
elif x_is_numeric or y_is_numeric:
    print('one of the values  is a valid number ')
else:
    print('none are valid numbers')
pppig
  • 1,215
  • 1
  • 6
  • 12
  • Hello! While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Brian61354270 Oct 21 '21 at 03:46