user10059620
user10059620

Reputation: 1

Why doesn't the != operator in python work for type the function (or is it just my code)?

I want to provide my code before asking the question for some context.

My code:

a = float(input('Insert the value for a: '))
b = float(input('Insert the value for b: '))
c = float(input('Insert the value for c: '))
if type(a) != (float() or int()):
print ('You didn\'t insert a number! Try again! This is your last chance or I will stop running!')
sleep(1)
print (a)
if type(b) != (float() or int()):
print ('You didn\'t insert a number! Try again! This is your last chance or I will stop running!')
sleep(1)
print (b)
if type(c) != (float() or int()):
print ('You didn\'t insert a number! Try again! This is your last chance or I will stop running!')
sleep(1)
print (c)

This outputs (given that I input values):

Insert the value for a: 8

Insert the value for b: 3

Insert the value for c: 2

You didn't insert a number! Try again! This is your last chance or I will stop running!

8.0

You didn't insert a number! Try again! This is your last chance or I will stop running!

3.0

You didn't insert a number! Try again! This is your last chance or I will stop running!

2.0

The problem is I specified that if it isn't a float or an integer, it should deliver the message. But I did insert an integer, but it still printed out the string. What's the problem? Can you assign variables to types of numbers?

Upvotes: 0

Views: 260

Answers (4)

vash_the_stampede
vash_the_stampede

Reputation: 4606

Its printing exactly what you are asking it to print

a = 1 

if a != 1 or a!=2:
    print('Of course, a != 2 is True!')
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 insert.py
Of course, a != 2 is True!

Only one of the or statements needs to evaluate to True for the if statement to execute, and since float != int it becomes True and runs print as you asked it to do

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71580

You want to do:

if type(...) not in (float, int):

Because need in operator for this, and no calling

Better:

if not isinstance(var,(float,int)):

Or inefficient way:

if type(...) is not float and type(...) is not int:

You can also do:

import sys
try:
    a = float(input('Insert the value for a: '))
    b = float(input('Insert the value for b: '))
    c = float(input('Insert the value for c: '))
except ValueError:
    print('Error: Not a integer or float') 
    sys.exit()

Upvotes: 2

ShadowRanger
ShadowRanger

Reputation: 155438

You called the float and int constructors, which, with no arguments, return the zero value.

So:

if type(a) != (float() or int()):

translates to:

if type(a) != (0.0 or 0):

which then (thanks to boolean evaluation rules) becomes:

if type(a) != 0:

which is clearly wrong.

If you want to test for precise types, check with in on a tuple of the types, e.g.:

if type(a) not in (float, int):

Normally you want to accept subclasses though, so the Pythonic approach would be:

if not isinstance(a, (float, int)):

Of course, none of this will actually solve your problem of checking. You explicitly created a by converting a str to a float. It's always going to be a float, or it's going to raise a ValueError if the string isn't a legal float value. The type checking will never help.

So what you really want is to perform the conversion in a try block and catch the exception if it fails:

try:
    a = float(input('Insert the value for a: '))
    b = float(input('Insert the value for b: '))
    c = float(input('Insert the value for c: '))
except ValueError:
    sys.exit('You didn\'t insert a number!')  # or some equivalent action to handle failure

If you want to loop until they give you a valid number, we have several questions to choose from (there are dozens more, I just can't be bothered to link them all).

Upvotes: 6

chepner
chepner

Reputation: 531345

!= works fine; the problem is that a != (b or c) does not mean a != b or a != c: != does not distribute over or. It has nothing to do with types.

Upvotes: 1

Related Questions