-1

I am new to Python. I have written a simple code to know the Data type of input. Here is my code. I gave 2 inputs that are getting convert as a "String". Using the "If" condition to match inputs data type. But my output is weird. I don't know why it is printing the only integer here. Can anyone help me to solve this?

I have added my output also here

Code:

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, type(b)==int):
print('A and B is Integer')

elif (a==str, b==str):
    print('A and B is String')

elif (a==float, b==float):
    print('\nA and B is Float')

print('\n*Program End*')'

Output:

Enter A Value 
abc
Enter B value 
def


A is =  abc
B is =  def

A and B is Integer

*Program End*
DarK_FirefoX
  • 887
  • 8
  • 20
  • 1
    Given that: 1. `input` gives you a string; and 2. you then explicitly pass that to `str`, why is it unclear what the data types of `a` and `b` are? – jonrsharpe Apr 05 '20 at 16:04
  • Does this answer your question? [How to determine a Python variable's type?](https://stackoverflow.com/questions/402504/how-to-determine-a-python-variables-type) – MShakeG Apr 05 '20 at 16:04
  • 2
    @jonrsharpe and all the comparisons are only testing the truthfulness of a non-empty tuple..... – Jon Clements Apr 05 '20 at 16:09
  • 1
    You may want to ``print((type(a)==int, type(b)==int))`` (note the required double parentheses) to see what you are actually checking. Also note that containers with at least one element are considered true. – MisterMiyagi Apr 05 '20 at 16:14
  • https://stackoverflow.com/users/5349916/mistermiyagi - I tried in your way. But still, my output is same "A and B is Integer" My question is : I know i gave string values there. But I want to match through "elif " statements and I wish to get the output as "A and B as String" there. – Dhulasiraman M Apr 05 '20 at 16:34
  • Does this answer your question? [How to check if input is float or int?](https://stackoverflow.com/questions/59807810/how-to-check-if-input-is-float-or-int) – MisterMiyagi Apr 05 '20 at 17:04
  • Does this answer your question? [What's the canonical way to check for type in Python?](https://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python) – Henry Harutyunyan Apr 06 '20 at 15:22

4 Answers4

2

(I'm not sure if this is what you're looking for) You can print the data type of something in python using the type() function like so:

var = 25
print(type(var))

Output:

<class 'int'>
Cerumersi
  • 77
  • 10
1

type() method will return class type

a=5
print(a,"is type of",type(a))
b=2.9
print(b,"is type of",type(b))
c='Hello'
print(c,"is type of",type(c))

Output:

5 is type of <class 'int'>
2.9 is type of <class 'float'>
Hello is type of <class 'str'>
Aditya
  • 17
  • 6
  • I want to match the data type with my input. I gave String as input. When I am using the "elif" statement, I am checking my input data type and expecting the output is only "String". But it is giving as "Integer" and program ends. – Dhulasiraman M Apr 05 '20 at 16:26
1

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().

DarK_FirefoX
  • 887
  • 8
  • 20
0
integer=100
print(integer,"is a",type(integer))
string="Python"
print(string,"is a",type(string))
decimal=5.973 #decimal is also called  as float.
print(decimal,"is a",type(decimal))

OUTPUT:

100 is a <class 'int'>
Python is a <class 'str'>
5.973 is a <class 'float'>
Code Carbonate
  • 640
  • 10
  • 18