Converting a input() to an int

def is_it_integer(prompt):
    n = prompt
       int(n) == n
       prompt = True
       return n
    except ValueError:
        prompt = False
        return prompt

count = 0

first_guess = input("Enter a number..:")

if is_it_integer(first_guess):
    first_guess = int(first_guess)

if count < first_guess:
    print("GO TO BED IT WORKED!")

Input these 3 characters before your code (```), left to 1 on my keyboard, but don’t include the brackets :smiley_cat: Hope this helps

1 Like

tried it, no luck. But thank you.

nevermind, totally worked, was using ‘’’, instead of ```

Nice, also you can do this: [code] … [/code] around the code.


Two things:

  1. int(n) == n is how you convert an int, however this line doesn’t really do anything. You would need to assign the result to something. x = int(n) == n
  2. You can really just do: return int(n) and ditch everything inside the try: before the except: If you attempt to convert something not an int it will throw the except, so then in your except: just return False.
1 Like

That looks so much better, and I get it now. Thanks!!!

I’m ignorant here because something I thought would work, doesn’t. I was wondering why we couldn’t just use the type() function to do something like this

if type(x) is int(1):
   print('x is an int')

but it fails, with either == or is.. 
>>> x = 1
>>> type(x) == int(1)
>>> type(x)
<class 'int'>
>>> type(int(1))
<class 'int'>
>>> type(x) is int(1)


Try this:

>>> type(10) is int
>>> type("10") is int