-2

I am brand new to coding and trying to learn Python. I cannot work out how to code an elif statement such that if the variable is an integer there is a certain output, but if it is not an integer then something else is printed. I keep getting an error. I am trying to write a program that can analyse the variable "x" and if x is a number it will print saying it is below 2 or 2 or more, but if x is anything else including a string, it will print "something else"

x = 2
if x < 2:
    print("Below 2")
elif x >= 2:
    print("Two or more")
else:
    print("Something else")

x = world

if x < 2:
    print("Below 2")
elif x >= 2:
    print("Two or more")
else:
    print("Something else")
  • try [`isinstance`](https://docs.python.org/3/library/functions.html#isinstance) – C.Nivs Feb 26 '21 at 17:17
  • 3
    Your problem. world must be in quotes, as it is a string. – SoJS Feb 26 '21 at 17:17
  • 2
    "_I keep getting an error_" You should tell us what the error is. – takendarkk Feb 26 '21 at 17:19
  • 1
    This code should throw loads of syntax errors that will point you in the right direction. Additionally, have a look at the `isinstance` function for determining types. – S3DEV Feb 26 '21 at 17:20
  • Does this answer your question? [Checking whether a variable is an integer or not](https://stackoverflow.com/questions/3501382/checking-whether-a-variable-is-an-integer-or-not) – wjandrea Feb 26 '21 at 17:31
  • Welcome to SO! Please take the [tour] and read [ask]. I suggested an existing thread that should answer your question. If it doesn't help, you'll need to [edit] your question, make the title more descriptive, and provide a [mre] including the full error message. – wjandrea Feb 26 '21 at 17:32
  • @S3DEV Syntax errors? No, there's only `NameError: name 'world' is not defined` – wjandrea Feb 26 '21 at 17:33
  • @wjandrea - Comparing a string to int using '>' should throw an error as well. (Does on my system) – S3DEV Feb 26 '21 at 18:01
  • @S3DEV True, though it doesn't get to that point with OP's code. And that's a `TypeError`. – wjandrea Feb 26 '21 at 18:19

1 Answers1

1

Here are a couple of different ways you might use to determine if x is an int:

x = "a"
# also try with x = 5

# option 1
if type(x) == int:
    print("int")
else:
    print("not int")

# option 2
if isinstance(x, int):
    print("int")
else:
    print("not int")
Adam Oellermann
  • 303
  • 1
  • 7
  • that is very helpful, thank you very much. I think I have now solved it based on your answer. See below, this works. is this most efficient way? `x = "word" if type(x) == int: if x < 2: print("Below 2") elif x >= 2: print("Two or more") else: print ("something else")` – newbie-star Feb 26 '21 at 17:30
  • 1
    It's bad practice to use `type()` cause it'll ignore subclasses. Prefer `isinstance()`. – wjandrea Feb 26 '21 at 17:36
  • @wjandrea - please could you write out how I would change my code to remove `type` and use `isinstance`? – newbie-star Feb 26 '21 at 17:42
  • 1
    You're probably OK with type if you're just dealing with "integers" vs "non-integers" - it seems rather unlikely that you will be subclassing int in your code! However, you would just change if type(x) == int: to if isinstance(x, int): - a straight line for line swap. – Adam Oellermann Feb 26 '21 at 17:46