-1

I've been trying to write a simple checking script for whatever the user inputs.

The script, however, automatically assumes that anything being input is a string by default. Is there a way to change that without using int(input) or float(input)? The point of the script is to use the isinstance to get the correct output.

l = input("Input: ")
if isinstance(l, int) and l >= 0:
    print ("l is %d and is more/equal to zero" % l)
elif isinstance(l, int) and l < 0:
    print ("l is %d and is lesser than zero" % l)
elif isinstance(l, float): 
    print ("l is %.2f and is a float variable" % l) 
elif isinstance(l, str):
    print ("l is %s and is a string" % l)
rind
  • 3
  • 2
  • 2
    `input` *always* returns a string. – deceze Mar 29 '22 at 10:02
  • 1
    "The script, however, automatically assumes that anything being input is a string by default." It is not *assuming* that. The thing being input **is** a string. It doesn't change just because of which keys the user decided to press. "Is there a way to change that without using int(input) or float(input)?" No (except for writing it yourself or using some other function that does the same thing etc.), for the same reason that you can't write the word `orange` on a page and then expect to eat it. What a string contains doesn't change the fact that it's a string. – Karl Knechtel Mar 29 '22 at 10:04
  • Keep in mind that the idea that "the symbol `1` followed by the symbol `0` represents the number ten" is actually entirely arbitrary - just like the idea that "the symbol `t` followed by the symbol `e` followed by the symbol `n` represents the number 10". You could make up any other rule on the spot. (After all, different bases exist, as do different human languages.) – Karl Knechtel Mar 29 '22 at 10:07
  • Seems like whatever you are trying to do can be achieved by catching the exceptions on typecasting - int() will raise an exception if the input does not map to an actual integer nicely, then you can check if it maps to float and finally leave it as string if both `int()` and `float()` raise an exception. – matszwecja Mar 29 '22 at 10:12

1 Answers1

0

The input() function always returns a string. If you want it to be some other type, you'll need to manually convert it.

my_str = input("Enter a number")
my_int = int(my_str)
my_float = float(my_str)
Miguel Guthridge
  • 1,444
  • 10
  • 27