Jun-02-2020, 11:38 PM
Whenever you input something like
x = input()Then the variable (
x
in this case) is always a string. In some of your other examples you're asking if it's a float, or an instance of a float, etc. It will never be. The contents might look like an integer, but the variable remains a string. You can attempt to cast that value it to a int or something else:x = input() i = int(x)But of course that can fail if the input text has characters in it. What your first one does is
try
to cast it this way, and print a message if that fails.x = input() try: i = int(x) [do stuff here with your int...] except ValueError: # Ooops, something in the try section failed with a valueerror. That likely # means the string didn't look like an int. We can complain here, and maybe # loop to retry. print(f"The input {x} doesn't seem to be a number. I don't know what to do with it.")