0

Examples of valid numbers: 123, 43.76, 89, 100000, 78765.987 (5 examples of valid number are given here, but there are infinitly many valid numbers)

Examples of strings (invalid numbers): 123abc, 7.9.8, afadf, *&^dadsf (4 string examples given here, but there are infinitly many such values which are not valid numbers)

How can I write a program that can tell the difference of a valid or non valid number

However my code is not working , this is the approach I came up with but if anybody can let me know of a better one that would be amazing , peace and good night on the eastcoast

#x = 8dhsdkjha and y = 88 output: both are valid numbers

which is false

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





 
Zak A
  • 13
  • 4
  • 2
    `if type(y) and type(x) == int or float` will always be true. – sj95126 Oct 19 '21 at 03:36
  • 1
    Please see [this question](https://stackoverflow.com/questions/36757965/how-to-have-multiple-conditions-for-one-if-statement-in-python) for multiple conditions in an if statement. For checking types, use `isinstance`, and `input` always returns strings. So `x` and `y` are both `str` type until you convert them. – C.Nivs Oct 19 '21 at 03:36
  • `type(input(":"))` -> `str` (class); and `"a" == false or true` -> always `true` (as it's parsed as `("a" == false) or true)`. – user2864740 Oct 19 '21 at 03:44

2 Answers2

0

You have to use the logical operators and and or each condition separately as you're doing in elif statement. So, your if statement needs to be like this

if  (type(y) == int and type(x) == float ) or (type(x) == float and type(y)== int):

Edit: Here, else part is always executed since input() returns string by default. You can add another elif statement to understand it.

elif type(x) == str and type(y) == str:
       print("Both are strings")



Python learner
  • 1,159
  • 1
  • 8
  • 20
  • The grouping on this is still wrong. `(type(y) == int and type(x))` is a `bool`, `bool == float` is always `False` – C.Nivs Oct 19 '21 at 14:24
  • @C.Nivs Yeah, you're correct. bool == float is always False. But, `==` operator has higher precedence than logical operators `and` and `or` – Python learner Oct 19 '21 at 16:43
  • @Zak A, check my updated answer. – Python learner Oct 19 '21 at 16:45
  • 1
    I'd like to amend my comment. The grouping is visually weird, but `isinstance(y, int) and type(x)` *does* actually return `type(x)` but only if the first check is correct. The parentheses give precedence in the type check here, the `== float` is done last, after the expression in the parentheses – C.Nivs Oct 19 '21 at 18:14
0

Why are you reinventing isnumeric?

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')

Not that we're really going for optimization here, but as to not have to run methods multiple times it can be useful (specifically in more complicated problems) to evaluate the truth up front and then compare bools.

For instance, if you had a class method that determined if an input was valid and it took 25 lines to do so, you would want to do something like

x = class.method(x)
y = class.method(y)
if x and y:
    foo
elif x or y:
    bar
else:
    zed

Which runs the method (25 lines) twice for 50 lines of execution.

Instead of:

if class.method(x) and class.method(y):
    foo
elif class.method(x) or class.method(y):
    bar
else:
    zed

Which runs the class method up to 4 times (100 lines).

This just expands if you do multiple elifs or multiple if blocks on the same input.

So ultimately I would suggest:

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

For execution optimization.

Or, for prettier code and optimized execution (that's still easy to read):

x = input(":").isnumeric()
y = input(":").isnumeric()
if x and y:
    print('both are valid numbers')
elif  x or y:
    print('one of the values  is a valid number ')
else:
    print('none are valid numbers')
Cfomodz
  • 532
  • 4
  • 17