0

I am trying to check if the input is integer. But the code is repetedly saying "something else" in each and every input I give. Is something wrong with the code?

x = input("enter:")
if type(x) == int:
    print("int")
else:
    print("something else")
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
IntoAbhi
  • 61
  • 1
  • 11
  • 6
    `type(input())` will **always be `str`**. Note, `type('123') is str` and `type(123) is int` – juanpa.arrivillaga Sep 20 '18 at 19:13
  • @juanpa.arrivillaga you don't want to edit the question _after_ it's been closed, because it automatically puts it in the reopen queue. – Jean-François Fabre Sep 20 '18 at 19:14
  • @Jean-FrançoisFabre didn't edit it – juanpa.arrivillaga Sep 20 '18 at 19:15
  • @Jean-FrançoisFabre ah yeah, I forgot I edited the tags, but I don't think that will put it in the reopen queue... whatever, my bad – juanpa.arrivillaga Sep 20 '18 at 19:17
  • If I could find the "real" one, if this is an exact dupe.... Thanks anyway. – IntoAbhi Sep 20 '18 at 19:29
  • @Jean-FrançoisFabre in full honesty. How is this a duplicate. OP isn't asking "how do I get an integer", they're asking what's wrong with the logic of the code. I'm sure they're perfectly capable of googling the other post and copy pasting blindly but it doesn't help them one bit. – Tasos Papastylianou Sep 20 '18 at 19:30
  • 2
    IntoAbhi just follow the link that has appeared above your question. Congrats on your first question, keep asking and don't be put off by the closing. Welcome to the community. – Tasos Papastylianou Sep 20 '18 at 19:31
  • 1
    @TasosPapastylianou I'm sure I can come up with a better duplicate. Still a duplicate. Edit: done ;) – Jean-François Fabre Sep 20 '18 at 19:32
  • This is where I was facing the problem: def game(): x = input("Enter the number: ") if type(x) == int: for i in range(2,x-1): if x%i == 0: primity = False break else: primity = True if primity == True: print("Prime") else: print("Not prime") game() else: print("Input a valid number you idiot!") game() game() This code always moves to the else statement. So I had to draft this question. – IntoAbhi Sep 20 '18 at 19:33
  • 1
    @IntoAbhi it's a duplicate but note that you didn't get any downvotes (for now). So it could have turned out worse :) Now don't paste your full code in comments, your [mcve] was enough. Read both provided links you'll get it. – Jean-François Fabre Sep 20 '18 at 19:34
  • @Jean-FrançoisFabre These many comments.... One single comment could've answered the question. – IntoAbhi Sep 20 '18 at 19:35
  • 1
    the top comment answers your question. the output of "input" will always be a string type. Therefore it's not an int type. Therefore your if statement always fails. – Tasos Papastylianou Sep 20 '18 at 19:37
  • comments aren't for answering. closing avoids too many duplicate answers too. – Jean-François Fabre Sep 20 '18 at 19:37
  • Thankyou @TasosPapastylianou – IntoAbhi Sep 20 '18 at 19:41

1 Answers1

-3
if float(x).is_integer():
   # do stuff

note, as the comments have said, x here is a string, so you're converting that string to a number first, then checking if it's an integer.

you may also want to wrap this around a try block to catch strings that are not numbers, etc.

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
  • 2
    why `float`? why not the usual way, `try: int(x) except: ValueError: ...` (I didn't downvote by the way) – juanpa.arrivillaga Sep 20 '18 at 19:16
  • int will fail for floats The idea is if you wanted to not throw an error for floats but just treat them differently. otherwise int() is also fine. Don't worry about the downvotes, I'm assuming people are just trying to close the question and are being rather dickish about it. Personally I don't think it's a duplicate because the user has a different problem and underlying mistake in logic, despite the common endgoal, so I'm not deleting in case it helps them. – Tasos Papastylianou Sep 20 '18 at 19:21
  • 1
    I downvoted, because this only handles strings that look like floats, because floating-point representation limitations cause incorrect output in many cases (Input consists of 1000 1s? False. Input is 9999999999999999.999? True.), and because if you're performing a "does this look like an int" check in the first place, you probably don't want to allow input with a decimal point in it. – user2357112 Sep 20 '18 at 19:22
  • Thanks. This is useful feedback (both for me and OP). – Tasos Papastylianou Sep 20 '18 at 19:24