0

I want to print different things when input() gets different data types. I have the following script:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

f = input("Please enter a number: ")

if f == type(int):
    print("That's good")
elif f == type(str):
    print("That isn't a number.")
else:
    print("BrainOverflow")

This script always returns the else part.

Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118

2 Answers2

2

Does not make any sense that you are trying since the result of the input() call is always a string. Your code needs some dedicated methods for checking if the string is representation of an integer or not. See

Python: Check if a string represents an int, Without using Try/Except?

Community
  • 1
  • 1
1

It's not recommended to do type checking in python, so you can try something like this:

f = input("Please enter a number: ")
try:
    f=int(f)
    print("That's good")
except ValueError:
    print("That isn't a number.")

or :

f = input("Please enter a number: ")
if f.isdigit():
    print("That's good")
else:
    print("That isn't a number.")
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504