The problem is on this line:
if (type(a)==int, type(b)==int)
^ this goes for all the if
conditions you have.
That is not the way to use multiple condition on an if
statement. In fact, that is just a tuple
. So you are saying if (0,0)
. So. According to documentation
By default, an object is considered true unless its class defines
either a bool() method that returns False or a len() method
that returns zero, when called with the object. 1 Here are most of the
built-in objects considered false:
- constants defined to be false: None and False.
- zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
- empty sequences and collections: '', (), [], {}, set(), range(0)
In this case you are using a tuple
with len() != 0
so it will always return True
when checked its truth value. So the correct way to check multiple truth conditions is using boolean operators:
a=str(input("Enter A Value \n"))
b=str(input("Enter B value \n"))
print('\n')
print('A is = ',a)
print('B is = ',b)
if type(a)==int and type(b)==int:
print('A and B is Integer')
elif type(a)==str and type(b)==str:
print('A and B is String')
elif type(a)==float and type(b)==float:
print('\nA and B is Float')
print('\n*Program End*')
^ Note I added type()
to the other conditions because they weren't present.
Now. There is another issue here:
a=str(input("Enter A Value \n"))
b=str(input("Enter B value \n"))
You are converting to str
the input, which is already an str
because input
give you str
, so you will always get:
A and B is String
Because they are both str
. So you could use str
built-in functions for this:
if a.isnumeric() and b.isnumeric():
print('A and B is Integer')
elif a.isalpha() and b.isalpha():
print('A and B is String')
elif a.replace('.','',1).isdigit() and b.replace('.','',1).isdigit():
print('\nA and B is Float')
The first one is a.isnumeric()
, then a.alpha()
and for last a workaround to check if it is float
: replace the .
for a 1 and check if it remains as isdigit()
.